Reputation: 865
I'm using paramiko to send an SSH command to a remote host, and check the resulting error code. Something like this:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=host, port=port, username=user)
chan = client.get_transport().open_session()
chan.settimeout(timeout)
chan.set_combine_stderr(True)
chan.get_pty()
chan.exec_command(command)
status = int(chan.recv_exit_status())
if status:
# got a non-0 exit status if I'm in here
sys.exit(status)
client.close()
This works great in that I am able to send SSH commands and it returns an error code of the process. But what if I am sending an SSH command that involves a pipe, such as 'echo "hello" | tee mylog.log'? In this case, the exit status returned as the 'status' variable in the code above, will be the exit status for tee, rather than the process piped in to it. (Obviously this echo command will not fail, but in reality I'm going to run a shell script and pipe in to tee, and it's the exit status of the shell script I'm curious about).
Now in a bash script I can just use ${PIPESTATUS[0]}. Is there any like this in paramiko? A way to use paramiko, and be able to pipe in to tee, and find out the exit status of the command piped?
Upvotes: 1
Views: 762
Reputation: 476
You can use set -eo pipefail
in the bash script that you are running through paramiko. In that case ; an appropriate exit status will be sent.
Also event if running a single command one can prefix the command with set -eo pipefail;{command}
Upvotes: 1