This page includes examples for authenticating against the /services/auth/ endpoint.
Command LineFollowing is an example using the wget command and HTTP digest to authenticate and request a token.
wget -O - -q --no-check-certificate --post-data="username=admin&password=changeme" https://localhost:8089/services/auth/login/
Pass wget POST data by using the --post-data= option, which takes a user defined string. The quotes are necessary to avoid passing the & character to the shell's command parser.
Run this from the command line to get an XML response containing your sessionKey:
wget -O - -q --no-check-certificate --post-data="username=admin&password=changeme" https://localhost:8089/services/auth/login/ <response> <sessionKey>a64fd1c2a24a31285b7add21ee6c9105</sessionKey> </response>
Extract the token completely from the XML and stick it in a file:
wget -O - -q --no-check-certificate --post-data="username=admin&password=changeme" https://localhost:8089/services/auth/login/ |egrep -o '[a-z0-9]{32}' > splunk_token.txt
cat splunk_token.txt
fe692f8c759027af4664b02912ec333fUse the Python SDK to authenticate against your Splunk server.
import splunk.auth as au import splunk.search as se
key = au.getSessionKey('admin','changeme')This example uses the default admin/changeme login. Update this for your instance. The getSessionKey method automatically caches the session key in the current interactive session, so you don't have to pass it along to subsequent methods. In a production implementation, or if you are connecting to multiple servers, you'll need to keep track of separate session keys.
If you have installed Splunk with the default settings, then your hostpath is https://localhost:8089. The client library knows this default, so you can authenticate directly by providing a username and password. If your server is on a different hostname or port, then you need to first update the session defaults:
splunk.mergeHostPath('splunk_hostname:12000', True)
key = au.getSessionKey('admin','changeme')The mergeHostPath method takes host information in many different forms:
This example uses Python and the httplib2 library. You may need to install it for your particular Python instance.
from httplib2 import Http
from urllib import urlencode
import xml.dom.minidom as xml
#
# set variables
#
endpoint = 'https://localhost:8089'
authMethod = endpoint + '/services/auth/login/'
authData = {'username': "admin", 'password': "changeme"}
h = Http()
resp, content = h.request(authMethod, "POST", urlencode(authData))
xmlDoc = xml.parseString(content)
tokenElements = xmlDoc.getElementsByTagName('sessionKey')
if not tokenElements:
print 'No session key found!'
tokenElements = xmlDoc.getElementsByTagName('msg')
print 'Reason=%s' % tokenElements[0].firstChild.nodeValue
else:
sessionKey = tokenElements[0].firstChild.nodeValue
print 'sessionKey=%s' % sessionKeySave this as something like first_post.py and then run it:
python first_post.py sessionKey=f7242c757db3f85e4a068af7727cf462
If the server authentication fails you'll get something that looks like this:
python first_post.py No session key found! Reason=Login failed
Comments
No comments have been submitted.