1
votes

What I am trying to do is perform a search on Splunk's API using python, I am able to get a session key but thats it. I'm new to both python and splunk so im a bit out-of-depth and any help would be really appreciated.

The error:

Traceback (most recent call last):
      File "splunkAPI.py", line 31, in <module>
        sid = minidom.parseString(r.text).getElementsByTagName('sid')[0].firstChild.nodeValue
    IndexError: list index out of range

python:

import time # need for sleep
from xml.dom import minidom

import json, pprint

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

base_url = 'https://___________:8089'
username = '______'
password = '______'
search_query = "____________"


#-------------------------get session token------------------------
r = requests.get(base_url+"/servicesNS/admin/search/auth/login",
        data={'username':username,'password':password}, verify=False)

session_key = minidom.parseString(r.text).getElementsByTagName('sessionKey')[0].firstChild.nodeValue
print ("Session Key:", session_key)

#-------------------- perform search -------------------------

r = requests.post(base_url + '/services/search/jobs/', data=search_query,
        headers = { 'Authorization': ('Splunk %s' %session_key)},
        verify = False)

sid = minidom.parseString(r.text).getElementsByTagName('sid')[0].firstChild.nodeValue

done = False
while not done:
        r = requests.get(base_url + '/services/search/jobs/' + sid,
                headers = { 'Authorization': ('Splunk %s' %session_key)},
                verify = False)
        response = minidom.parseString(r.text)
        for node in response.getElementsByTagName("s:key"):
                if node.hasAttribute("name") and node.getAttribute("name") == "dispatchState":
                        dispatchState = node.firstChild.nodeValue
                        print ("Search Status: ", dispatchState)
                        if dispatchState == "DONE":
                                done = True
                        else:
                                time.sleep(1)

r = requests.get(base_url + '/services/search/jobs/' + sid + '/results/',
        headers = { 'Authorization': ('Splunk %s' %session_key)},
        data={'output_mode': 'json'},
        verify = False)

pprint.pprint(json.loads(r.text))
1

1 Answers

1
votes

Hmm... that code looks awfully familiar :P Unfortunately, error checking wasn't that important when I wrote it.

The issue you see occurs if the search_query is not defined properly. It must start with search=. Also note that you need to include an initial search command if doing a standard Splunk search,

For example, search=search index=* will work, search=index=* will not work.

If you need to include quotes in your search string, I suggest you use something like the following format.

search_query = """search=search index=* "a search expression" | stats count"""