Splunk Dev

Unable to reference .splunkrc file from Splunk Python SDK

nsshey
Explorer

I created the .splunkrc file and correctly stored it in C:\Users\username.splunkrc
I filled it out with correct credentials.

1) I am trying to access KV store (the credentials work when I enter via cmd line but not when using .splunkrc😞

opts = parse(sys.argv[1:], {}, ".splunkrc")
opts.kwargs["owner"] = "nobody"
opts.kwargs["app"] = "search"
service = client.connect(**opts.kwargs)

I get this error: socket.error: [Errno 10061] No connection could be made because the target machine actively refused it

2) How do I use the .splunkrc for a regular search query connect? I am currently doing this (hardcoded data):

# Create a Service instance and log in 
service = client.connect(
    host=HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)
0 Karma

jwalthour
Communicator

Create a file called ".splunkrc" with the following in your $HOME directory:

host=localhost
port=8089
username=restclient
password=splunkC0der

Then, run the following as your splunk user:

splunk add user restclient -password "splunkC0der" -full-name 'REST client' -role admin

Finally, you should be able to create the following login.py file:

import splunklib.client as client
import sys

if len(sys.argv) != 5:
        print "Syntax: login.py username password hostname port"
        sys.exit()

uid  = sys.argv[1];
pwd  = sys.argv[2];
host = sys.argv[3];
port = sys.argv[4];

try:
        service = client.connect(host=host, port=port, username=uid, password=pwd)

        print
        print 'Authenticated, token = [%s]' % service.token
        print

        service.logout()

except Exception, err:
        sys.stderr.write('\nError: %s\n\n' % str(err))

and run it successfully:

python login.py
0 Karma

Ralf
Explorer

Hi jwalthour,

I'm new here and found your response here in community (even when far in the past) as I have same issue that .splunkrc file is not recognized.
My understanding was, that if I have this file correctly setup and stored, I do not need to enter any login data
to any Python script at all.

But if I understood your Python example correctly, it ask actively to enter credentials and host/port 
when I execute login.py

 

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...