Splunk Dev

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!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...