title: Can I get the exit code of a command executed in a subshell via ssh?
body: <p>I'm trying to use Paramiko to write a deployment script, and I'm having trouble with exit codes from the commands I run. I'm using code similar to that in <a href="https://stackoverflow.com/questions/1911690/nested-ssh-session-with-paramiko/1916877#1916877" title="Nested SSH session with Paramiko">this answer</a>, but it's a little more complicated. Basically, from our dev boxes we have to go through a jump server, and from there to a series of production machines. Once there we have to switch to a system user (sudo su - systemuser) and then we can run commands. </p>
  
  <p>The problem is that as I understand it I have 3 subshells - the ssh session, the nested ssh command and then the su subshell. I can't get Paramiko to give me back the exit code of the commands in the inner subshell - I guess the exit code it will eventually return will be that of the ssh command. I suspect this problem is not actually specific to Paramiko - does the SSH protocol even support this kind of usage?</p>
  
  <p>I'm currently always executing:</p>
  
  <pre><code>(my command); echo "Process terminated with exit code $?"
  </code></pre>
  
  <p>and then parsing this out on the client, but it's pretty ugly - is there a nicer way?</p>