Getting Data In

Why am I getting "IndexError: list index out of range" trying to get the session key in a Splunk REST API call?

nikhiltikoo
Explorer

I am trying to get the result of a search from Splunk, but when I try to get the session key, I am getting the following error.

Traceback (most recent call last): File "splunkenter.py", line 18, in sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue IndexError: list index out of range

Can anybody tell me where is the problem lying? I am very new to API's.

0 Karma

alacercogitatus
SplunkTrust
SplunkTrust

Assuming you are writing a scripted input or modular input, you can read the session key like this:

config_str = sys.stdin.read()
doc = xml.dom.minidom.parseString(config_str)
root = doc.documentElement
sessionkey = root.getElementsByTagName("session_key")[0].firstChild.data
config["session_key"] = sessionkey

The error you are seeing is that there doesn't exist an array for one of the elements. So either getElementsByTagName('sessionKey')[0] has no data or childNodes[0] has no data. Those would be the two arrays that would cause that error. Try splitting the call apart into different variables and isolate which one is not returning correctly.

0 Karma

nikhiltikoo
Explorer

I am not reading anything from the standard input. I just want to obtain the session key for the on going session.
config_str = sys.stdin.read() -> This prompts user to input something.
Whereas i don't want to input anything. I am just trying to retrieve the session id for the session in order to connect to the splunk.

0 Karma

nikhiltikoo
Explorer

This is the code that i am using

import urllib
import httplib2
import time
import re
from time import localtime,strftime
from xml.dom import minidom
import json
baseurl = 'https://localhost:8089'
username = ''
password = ''
myhttp = httplib2.Http()

servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST',
headers={}, body=urllib.urlencode({'username':username, 'password':password}))[1]
sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue
print "====>sessionkey: %s <====" % sessionkey

0 Karma

alacercogitatus
SplunkTrust
SplunkTrust

Ok, so now you have another array. myhttp.request(baseurl + '/services/auth/login', 'POST',
headers={}, body=urllib.urlencode({'username': username, 'password': password}))
now might not have a 1 index in it. Try using exceptions to figure it out.

try:
    servercontent = myhttp.request(baseurl + '/services/auth/login', 'POST',
                           headers={}, body=urllib.urlencode({'username': username, 'password': password}))[1]
except:
    print "error in retrieving login."
try:
     sessionkey = minidom.parseString(servercontent).getElementsByTagName('sessionKey')[0].childNodes[0].nodeValue
     print "====>sessionkey: %s <====" % sessionkey
except:
     print "error in retrieving sessionkey"
0 Karma
Get Updates on the Splunk Community!

Developer Spotlight with William Searle

The Splunk Guy: A Developer’s Path from Web to Cloud William is a Splunk Professional Services Consultant with ...

Major Splunk Upgrade – Prepare your Environment for Splunk 10 Now!

Attention App Developers: Test Your Apps with the Splunk 10.0 Beta and Ensure Compatibility Before the ...

Stay Connected: Your Guide to June Tech Talks, Office Hours, and Webinars!

What are Community Office Hours?Community Office Hours is an interactive 60-minute Zoom series where ...