Assistance with testing ssh connections and what the return codes mean

Hello Forum,

I'm using a bit of code from a script I found that allows me to capture the status code of connecting via SSH to remote servers:

ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 user@$InputIP 'ls -l /home/user >/dev/null 2>&1' > /dev/null 2>&1

status="$(echo $?)"

echo $status

When I echo out the status codes I can see codes such as the following:

0 - Success
255 - Error Can't login (not sure why though)
1 - Password Expired

The reasons for each I've gathered from my own experience and from notes in the script I'm using that listed these reasons. But could someone point me to documentation that would help me identify more definitively what the reason codes mean when I connect through SSH and get a return code.

Thank you.

From man ssh :

EXIT STATUS
     ssh exits with the exit status of the remote command or with 255 if an
     error occurred.

so:

$ ssh non-existant-user@mymachine
non-existant-user@mymachine: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
$ echo $?
255

$ ssh mymachine exit 3
$ echo $?
3

Don't know why you get exit status of 1 when the password has expired - perhaps this is what login returns on target OS when password expired.

1 Like

Thanks very much for this reply! I've looked at the MAN pages for SSH and I'm seeing the following Error codes listed on that site:

SSH Error Codes
0
Success
1
Generic error
2
Remote host connection failure

But when I connect to some servers I'm seeing more error codes like 5 which isn't listed as an error code? Is there a list of possible error codes complied somewhere or is what's listed on the MAN page the full list?

Thank you.

The ssh client exit codes seem to be vary a fair bit between different implementations.

It's unfortunate the the online manual appears to be incorrect or old on your target system. You could try ssh -V or ssh --version and then look for some on-line manuals/doco, or even the source for that particular client version.

1 Like

Thanks again Chubler_XL for taking the time to reply to my post. We do have a plethora of versions still in use from RHEL 6.9 all they up to 7.7.

I did notice though on a system where I had to change the password...when I login manually I immediately get a message that the password has expired. Is there a way upon first connecting to a system via ssh to capture some reply back that may be used to identify the connection? Here's what I currently have in my script where I check my ssh connection and define a status code based on that initial connection:

ssh -qno StrictHostKeyChecking=no -o ConnectTimeout=1 user@$InputIP 'ls -l /home/user >/dev/null 2>&1' > /dev/null 2>&1
        status="$(echo $?)"
        echo $status

I then have defined in my script the following status based on the return code from $status above:

UNREACHABLE="255"
INVALID_PASSWORD="5"
NO_HOME_DIRECTORY="2"
PASSWORD_EXPIRED="1"
SUCCESS="0"

Does the above code look like this could work for me? Is there perhaps a better way to identify a password has expired on a system using our ID?

Thanks!