I want to check the status of <SERVICE>
on QEMU instance (standard connection is ssh [email protected]
).
When I do
sshpass -p none ssh -o StrictHostKeyChecking=no [email protected] -t 'systemctl is-active <SERVICE>'
I got expected output:
failed
If I try to use subprocess.check_output
for the same as
subprocess.check_output("sshpass -p none ssh -o StrictHostKeyChecking=no [email protected] -t 'systemctl is-active <SERVICE>'", shell=True, encoding='utf8', stderr=subprocess.DEVNULL)
I got
Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.8/subprocess.py", line 415, in check_output return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, File "/usr/lib/python3.8/subprocess.py", line 516, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command 'sshpass -p none ssh -o StrictHostKeyChecking=no [email protected] -t 'systemctl is-active <SERVICE>'' returned non-zero exit status 3.
But if I use the same code-line with any other command, e.g.
subprocess.check_output("sshpass -p none ssh -o StrictHostKeyChecking=no [email protected] -t 'echo $PATH'", shell=True, encoding='utf8', stderr=subprocess.DEVNULL)
I got expected output:
'/usr/bin:/bin:/usr/sbin:/sbin\n'
So why subprocess.check_output
doesn't work correctly with systemctl
?
I got the service status by replacing
check_output
method withrun
asbut that looks more like a workaround since
check_output
should be used specifically for getting the command output andrun
is general method to execute commands...Update
Also
getstatusoutput
works asHowever, it returns exitcode along with the status
so required output should be parsed additionaly