Getting Data In

Connecting to splunk server using python script from Python IDLE shell but data is not displaying?

Explorer

import splunklib.client as client

def setServer(HOST, PORT, USERNAME, PASSWORD):
HOST = "hostname"
PORT = 8089
USERNAME = "User"
PASSWORD = "Password"
service = client.connect(host=HOST,port=PORT,username=USERNAME,password=PASSWORD)
for app in service.apps:
print app.name

After executing this script not showing any output?Please help how to resolve

Tags (2)
0 Karma

SplunkTrust
SplunkTrust

Hi @ramesh12345,

I don't see in your code that you are calling setServer function in your python script after defining it.

Please find below script which is displaying correct data.

import sys
import getpass
sys.path.append('splunk-sdk-python-1.6.4')
import splunklib.client as client

def setServer(hostname, splunkuser, splunkpassword):
    HOST = hostname
    PORT = 8089
    USERNAME = splunkuser
    PASSWORD = splunkpassword
    service = client.connect(host=HOST,port=PORT,username=USERNAME,password=PASSWORD)

    for app in service.apps:
        print app.name

if __name__ == "__main__":
    hostname = raw_input("Enter Splunk Hostname/IP: ")
    splunkUser = raw_input("Enter Splunk Admin Username: ")
    splunkPassword = getpass.getpass("Enter Splunk Admin Password: ")
    setServer(hostname, splunkUser, splunkPassword)

When you run above script with Splunk Python SDK you need to enter 3 pramater value, so when you will run above script you will able to see below prompts (You can remove those prompts and pass hostname, username and password hardcoded or pass it from some other function based on your requirement.

$ python test.py
Enter Splunk Hostname/IP: localhost
Enter Splunk Admin Username: admin
Enter Splunk Admin Password:
alert_logevent
alert_webhook
appsbrowser
framework
gettingstarted
introspection_generator_addon
launcher
learned
legacy
sample_app
search
splunk_archiver
splunk_httpinput
splunk_instrumentation
splunk_monitoring_console
SplunkForwarder
SplunkLightForwarder

I hope this helps.

0 Karma