alexsiegman.com - linuxhttp://alexsiegman.com/2013-03-27T00:00:00-05:00Using SSH in scripting and understanding it’s exit code2013-03-27T00:00:00-05:002013-03-27T00:00:00-05:00Alex Siegmantag:alexsiegman.com,2013-03-27:/2013/03/27-ssh-scripting/<p>As a Linux Systems Administrator I do quite a bit of scripting, and oftentimes I’m doing much of this in bash especially when using Jenkins or Hudson to give a nice interface and accessibility to build and deployment for the shop I’m working with. Often times, you want …</p><p>As a Linux Systems Administrator I do quite a bit of scripting, and oftentimes I’m doing much of this in bash especially when using Jenkins or Hudson to give a nice interface and accessibility to build and deployment for the shop I’m working with. Often times, you want these jobs to fail if there’s a problem with any step along the way, but you’re doing steps where you need to ssh to other servers and run commands, or something similar.</p>
<p>In a script, ssh’s exit code will be that of the last executed command. Executed is the key word there; there are times when it will continue to run other commands as well. I wrote a quick gist to illustrate the point.</p>
<p><a href="https://gist.github.com/asiegman/5256589"><a href="https://gist.github.com/asiegman/5256589"><a href="https://gist.github.com/asiegman/5256589">https://gist.github.com/asiegman/5256589</a></a></a></p>
<p>The main thing to remember here is to use the right combination of multiple-line ssh formatting and environment settings. Also to remember, the script you’re running it from will not pick up any errors from inside the <span class="caps">SSH</span> connection if you’re using the “set -e” option, since the only command actually run from that script’s shell is the <span class="caps">SSH</span> command.</p>
<p>Hope that helps!</p>