Getting Data In

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

ramesh12345
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

harsmarvania57
Ultra Champion

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
Get Updates on the Splunk Community!

Prove Your Splunk Prowess at .conf25—No Prereqs Required!

Your Next Big Security Credential: No Prerequisites Needed We know you’ve got the skills, and now, earning the ...

Splunk Observability Cloud's AI Assistant in Action Series: Observability as Code

This is the sixth post in the Splunk Observability Cloud’s AI Assistant in Action series that digs into how to ...

Splunk Answers Content Calendar, July Edition I

Hello Community! Welcome to another month of Community Content Calendar series! For the month of July, we will ...