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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Mile High Learning with Splunk University, Denver, Colorado

If Denver is known for its mile-high elevation, Splunk University is about to raise the bar on technical ...

IT Service Intelligence 5.0 Series: Your Guide to the June Launch

We are excited to announce the June release of Splunk IT Service Intelligence (ITSI) 5.0. This update ...

Agent Mode Engaged! Enchaining Agentic Operations with Splunk AI Assistant 2.0

    Are you ready to transform how your team handles complex data requests? We invite you to our upcoming ...