Client connect issue with adding error handling

Viewed 57

I am trying to connect to an account on Splunk via Python and Bash. I can connect to the website fine and it prints what I want it to, in the terminal, when I log in correctly. However when I use the wrong log in details, it prints a large error message saying 'login failed' that I want to try and condense to one line only. This is what I am using to connect to Splunk:

service = client.connect(
    host=splunk_config['host'],
    port=splunk_config['port'],
    username=splunk_config['username'],
    password=splunk_config['password'])

I want to do something along the lines of:

if (service errors):
   print ("Failed to connect")
else:
   print ("Successfully connected")
1 Answers

Without the exception and guessing you're using splunklib I would imagine you need something like:

try:
    service = client.connect(
        host=splunk_config['host'],
        port=splunk_config['port'],
        username=splunk_config['username'],
        password=splunk_config['password'])
    print("Login succesfull")
except splunklib.binding.AuthenticationError as e:
    print("Login failed")
Related