Python - Capture exit status of command executed via SSH -


i need way capture exit status command run through ssh. exit status end within variable. cannot seem working though.

command simple like:

os.system("ssh -qt hostname 'sudo yum list updates --security > /tmp/yum_update_packagelist.txt';echo $?")

anyone have idea? i've tried has either not worked @ all, or ended giving me exit status of ssh command, not underlying command.

you want use ssh library paramiko (or spur, fabric, etc.… google/pypi/so-search "python ssh" see options , pick 1 best matches use case). there demos included paramiko want do.

if insist on scripting command-line ssh tool, (a) want use subprocess instead of os.system (as os.system docs explicitly say), , (b) need bash-scripting (assuming remote side running bash) pass value (e.g., wrap in one-liner script prints exit status on stderr).

if want know why existing code doesn't work, let's take @ it:

os.system("ssh -qt hostname 'sudo yum list updates --security > /tmp/yum_update_packagelist.txt';echo $?") 

first, you're running ssh command, separate echo $? command, echo exit status of ssh. if wanted echo status of sudo, need semicolon ssh command. (and if wanted status of yum, inside sudo.)

second, os.system doesn't @ gets printed stdout anyway. docs say, "the return value exit status of process". so, you're getting exit status of echo command, pretty guaranteed 0.'

so, make work, you'd need echo right place, , read stdout using subprocess.check_output or similar instead of os.system, , parse output read last line. if that, should work. again, shouldn't that; use paramiko or ssh library.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -