1

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?

1
  • I haven't forgotten about your answer, and thanks for taking the time to post it. I just had to shift projects momentarily. I will be checking this tomorrow and will follow up suitably. Thank you! Commented Mar 14, 2018 at 4:59

1 Answer 1

1

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}

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

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.