Building for the Splunk Platform

How do I generate a session token that I can use in client.connect of python SDK?

hsingams2
Explorer

I'm using splunklib python SDK to connect to a splunk instance but I don't want to explicitly specify username/password in plain text in the python script. I see client.connect() function has a token argument but I am not sure how to generate this token.

import splunklib.client as client
service = client.connect(
host=HOST,
port=PORT,
username=USERNAME,
password=PASSWORD)

What is the procedure for generating the token (i.e. session_key)?

Tags (1)
0 Karma

rroberts
Splunk Employee
Splunk Employee

You could pass the arguments at run time : For example...

import splunklib.client as client
import sys

if len(sys.argv) != 5:
print "Syntax: login.py username password hostname port"
sys.exit()

uid = sys.argv[1];
pwd = sys.argv[2];
host = sys.argv[3];
port = sys.argv[4];

try:
service = client.connect(
username=uid,
password=pwd,

host=host,
port=port)
context=service.login()

    print 'Authenticated, token = [%s]' % service.token     

except Exception, err:
sys.stderr.write('Error: %s\n' % str(err))

0 Karma

hsingams2
Explorer

Thanks for the reply. I understand I can pass the credentials using the command line args but my intent was not that. What I was looking for is a way to generate a token offline and use it for client connect login (e.g., an approach similar to splunk HEC authentication token. I'm not suggesting I would use HEC token here, just referring to the concept)

0 Karma

rroberts
Splunk Employee
Splunk Employee

If you already have a token you can use the following construct.

Or if you already have a session token

s = client.Service(token="atg232342aa34324a")
Check out:
http://docs.splunk.com/DocumentationStatic/PythonSDK/1.5.0/client.html#splunklib.client.Service

0 Karma

hsingams2
Explorer

I don't have a session token and my original question is about - how do I generate one?

0 Karma

hsingams2
Explorer

sounds like, the only way to generate this is by logging in with the username/password and get it from service.token

0 Karma
Get Updates on the Splunk Community!

The Splunk Success Framework: Your Guide to Successful Splunk Implementations

Splunk Lantern is a customer success center that provides advice from Splunk experts on valuable data ...

Splunk Training for All: Meet Aspiring Cybersecurity Analyst, Marc Alicea

Splunk Education believes in the value of training and certification in today’s rapidly-changing data-driven ...

Investigate Security and Threat Detection with VirusTotal and Splunk Integration

As security threats and their complexities surge, security analysts deal with increased challenges and ...