Alex Bligh
2015-Sep-11 15:25 UTC
Differentiating between ssh connection failures and ssh command failures
I'm sure this should be an easy question, but from the ssh client manpage: EXIT STATUS ssh exits with the exit status of the remote command or with 255 if an error occurred. Let's say I'm using ssh server.example.com /usr/bin/do/something in (e.g.) a bash script. How can one differentiate between a failure of ssh to connect to the host and the command in question returning an error? I need to detect both, and differentiate between them. -- Alex Bligh
Damien Miller
2015-Sep-12 01:14 UTC
Differentiating between ssh connection failures and ssh command failures
On Fri, 11 Sep 2015, Alex Bligh wrote:> I'm sure this should be an easy question, but from the ssh client manpage: > > EXIT STATUS > ssh exits with the exit status of the remote command or with 255 if an error occurred. > > > Let's say I'm using > ssh server.example.com /usr/bin/do/something > in (e.g.) a bash script. > > How can one differentiate between a failure of ssh to connect to the host and the > command in question returning an error? I need to detect both, and differentiate > between them.ssh server.example.com /usr/bin/do/something r=$? if [ $r -eq 0 ] ; then echo success elif [ $r -eq 255 ] ; then echo ssh failed else echo command failed fi
Stephen Harris
2015-Sep-12 02:13 UTC
Differentiating between ssh connection failures and ssh command failures
On Sat, Sep 12, 2015 at 11:14:07AM +1000, Damien Miller wrote:> ssh server.example.com /usr/bin/do/something > r=$? > if [ $r -eq 0 ] ; then > echo success > elif [ $r -eq 255 ] ; then > echo ssh failed > else > echo command failed > fissh remoteserver exit 255 Hmm :-) exit(-1) aka exit(255) is a pretty standard "generic failure code" for many programs. The problem, really, is that "exit code" is the wrong thing to test for. x=`ssh remoteserver "echo CONNECTED && somecommand"` And then see if CONNECTED appears in the output to show successful connection. -- rgds Stephen