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!

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...