Topics

| pdf version

Splunk > The IT Search Company

  • Search and navigate IT data from applications, servers and network devices in real-time.
  • Download Splunk

Localized Splunk documentation

Looking for Splunk documentation in other languages?

Python example

This documentation applies to the following versions of Splunk: 4.0 , 4.0.1 , 4.0.2 , 4.0.3 , 4.0.4 , 4.0.5 , 4.0.6

Python example

Here's an example of authenticating against a Splunk server and running a search query in Python.

#!/opt/splunk/bin/python -u

import urllib
import httplib2
from xml.dom import minidom

baseurl = 'https://localhost:8089'
userName = 'admin'
password = 'changeme'

searchQuery = 'sourcetype=access_common | head 5'

serverContent = httplib2.Http().request(baseurl + '/services/auth/login',
'POST', headers={}, body=urllib.urlencode({'username':userName, 'password':
password}))[1]
sessionKey =
minidom.parseString(serverContent).getElementsByTagName('sessionKey')[0].chi
ldNodes[0].nodeValue

# check if the query has the search operator
if not searchQuery.startswith('search'):
 searchQuery = 'search ' + searchQuery

print httplib2.Http().request(baseurl + '/services/search/jobs','POST',
headers={'Authorization': 'Splunk %s' %
sessionKey},body=urllib.urlencode({'search': searchQuery}))[1]
Revision: 207 | Contact | Privacy Policy | Terms of Use | Community content licensed under Creative Commons