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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...