Can I get the exit code of a command executed in a subshell via ssh?

I am trying to use Paramiko to create a deployment script, and I am having problems with the exit codes from the commands that I run. I am using code similar to the code in this answer , but it is a bit more complicated. Basically, from our dev boxes we have to go through the jump server, and from there to a series of production machines. After that, we must switch to the system user (sudo su - systemuser), and then we can run the commands.

The problem is, as I understand it, I have 3 subshells - an ssh session, a nested ssh command, and then su a subshell. I can't get Paramiko to give me the exit code for the commands in the inner subshell - I think the exit code that he will eventually return will match the ssh command. I suspect that this problem is not really typical for Paramiko - does the SSH protocol support this kind of use?

I always do:

(my command); echo "Process terminated with exit code $?" 

and then parsing it on the client, but it's pretty ugly - is there a better way?

+6
ssh paramiko
source share
2 answers

I think the exit code that he will eventually return will be the same as in the ssh command. I suspect that this problem is not really typical for Paramiko - does the SSH protocol support this kind of use?

Yes and no.

The return status is always returned, but it may not be what you think. Once you call invoke_shell() , your remote process is a shell, possibly bash. The resulting exit_status will be the one of them, not any process. Paramiko (or any ssh implementation) will never be able to return the exit status of your commands, because the exit statuses are never visible outside this remote shell. You decide what should be for your exit status, and exit the shell using exit $EXIT_STATUS (often exit $? enough)

If you can break your commands or wrap them in a script and then use the exec_command() method, you will have access to the correct exit state.

Alternatively, you can use the exec shell command in the remote shell, and the shell process will be replaced by the command called exec and its exit status will be returned.

All three of these methods will set the channel.exit_status attribute as expected.

+8
source share

I had a problem when the man page for 'ssh' says that it returns the exit code of the process that it starts, but I can not get it to return a non-zero error code. On the ssh man page:

  The session terminates when the command or shell on the remote machine exits and all X11 and TCP/IP connections have been closed. The exit sta‐ tus of the remote program is returned as the exit status of ssh. 

This is not true.

But I would try something like this and see what happens on your system.

 % ssh localhost bash -c "exit 3" ; echo $? 0 

When I run a similar command locally, bash returns the exit code.

 % bash -c 'exit 3' ; echo $? 3 

Double quotes will be removed before ssh displays the commands. So let's try more quotes.

 % ssh localhost bash -c "'exit 3'" ; echo $? 3 

Bingo. "Exit 3" turned into an "exit", followed by an ignored word on the bash command line. So bash ran the "exit" command.

Unfortunately for me, I think that this whole answer is a departure from the original questions and does not contain sufficient merit, since the question is by itself. So thank you all for helping me answer the second question (not related to the original question).

+2
source share

All Articles