1

I am new to python and working on trying to make a script which checks if a specified host as for example sensu-client exist. I use a deployment software called NSO and run it by: nso status and it shows me this information:

nagios-client host nagios-client down
test host test down

Is there any possibility to make a script to check if for example nagios-Client exist with a script ?

In shell I do it by:

nso status | awk '{ print $1 }'
1
  • I'm not sure what you're asking. Can you give more details about what problem you're having? Commented Feb 21, 2015 at 16:11

3 Answers 3

1

In this case I would suggest using subprocess' check_output function. The documentation is here. check_output can return, as a string the shell output of a command. So you would have something like this:

import subprocess
foo=subprocess.check_output(['nso', 'status', '|', 'awk', '\'{ print $1 }\''], shell=True) 
#Thanks bereal for shell=True
print foo

Of course, if your only targeting linux, you could use the much easier sh module. It allows you to import programs as if they were libraries.

Sign up to request clarification or add additional context in comments.

1 Comment

You'll need to add shell=True for this pipe to work.
1

you can use subprocess to run this command and parse the output

import subprocess
command = ['nso', 'status', '|', 'awk', '\'{ print $1 }\'']
p1 = subprocess.Popen(command, stdout=subprocess.PIPE)

1 Comment

This won't produce the desired result, you changed from command to first in your code, so you should probably pick one, otherwise it will throw an error
0

You don't have to run awk, since you're already in Python:

import subprocess
proc = subprocess.Popen(['nso', 'status'], stdout=subprocess.PIPE)

# get stdout as a EOL-separated string, ignore stderr for now
out, _ = proc.communicate()  

# parse the output, line.split()[0] is awk's $1
items = [line.split()[0] for line in out.split('\n')]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.