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!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...