1754922 Members
3052 Online
108827 Solutions
New Discussion

ssh return code

 
SOLVED
Go to solution
support_5
Super Advisor

ssh return code

Hi Guys,

Does anyone know how to trap ssh return code? For example, on server A, ssh to server B and run command X. I would like to trap the return code of command X.

The long way to this would be to trap the return code to a file when running command X on server B, then run a scp to grab that file from server A. But what if the command itself is another ssh or scp command?

Any ideas?

Thanks in advance for your time.

Tung
1 REPLY 1
Matti_Kurkela
Honored Contributor
Solution

Re: ssh return code

The manual page of OpenSSH says:

---begin quote---
DIAGNOSTICS
ssh exits with the exit status of the remote command or with 255 if an error occurred.
---end quote---

That means, if there's an error code, if it's 255 you handle it as "an error in SSH connection". If it's anything else, you do exactly what you'd do if the remote command had been local.

If you do something like
localhost% ssh user@host1 ssh user@host2 foobar

and get an error 255, you can't tell whether the error was from a ssh connection from the local host to host1 or from host1 to host2, unless you parse the error messages somehow.
MK