What are the exit statuses of ssh command?

by on February 9, 2006 · 4 comments· LAST UPDATED August 28, 2006

in

SSH client is a program for logging into a remote machine and for executing commands on a remote machine. By default in Linux/UNIX if particular command/shell script is executed, it return two type of values which is used to see whether command or shell script executed is successful or not. This value is know as Exit Status of shell command or script.

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 status of the remote program is returned as the exit status of ssh. You can use bash shell $? Variable to print exit status of shell. For example:

user@localhost: $ ssh user@somewhere.com

Password:

user@somewhere.com: $ exit
user@localhost: $ echo $?

In short,

  1. ssh exits with the exit status of the remote command which can be find with echo $? command.
  2. Or value 255 is return, if an error occurred while processing request via ssh session
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 Rusty January 30, 2009 at 5:21 pm

Very well, but what ARE the exit stati of the ssh command?
e.g, 0 if succesful? Or, 1?

Reply

2 wyrmmage July 16, 2009 at 12:02 am

Seeing as this is the top hit for googling “ssh return status”, I thought I’d go ahead and add that

The remote command may also terminate violently due to a signal.
Such a condition can be indicated by the following message. A zero
‘exit_status’ usually means that the command terminated successfully.
byte SSH_MSG_CHANNEL_REQUEST
uint32 recipient channel
string “exit-signal”
boolean FALSE
string signal name (without the “SIG” prefix)
boolean core dumped
string error message in ISO-10646 UTF-8 encoding
string language tag [RFC3066]

So you don’t *always* get the return status of your program.

Good post, though….it answered my question :)

Reply

3 Whatever September 16, 2011 at 12:02 pm

My SSH version returns 0 for okay, and 255 if connection failed.

Reply

4 josh June 15, 2012 at 9:23 am

if you looking for a way to return failure from a remote bash script, try:

kill $$

this causes bash to kill itself ($$ evaluates to bash’s pid) and ssh to return 255.

Reply

Leave a Comment

Tagged as:

Previous Faq:

Next Faq: