OSETC TECH

How to Check the EXIT Status From a SSH Remote Command in Linux Bash

This post will guide you how to get the exit status from a ssh remote command in Linux bash. How do I find the exit code of a remote command executed via ssh commmand under Linux system.

Checking EXIT Status of SSH Command


If you want to execute a command from a remote Linux server, you can use the ssh command to connect that remote server, and pass one command that you want to run to ssh command.

After SSH command exits,  you need to check if that command executed via ssh command is successful or not. How to check it. You need to check the exit status of ssh command from remote server.

As the ssh client will exit with the exit code of the remote command, so you can check the value of $?. if it return 0, it indicates that command is executed successfully.

For example, you want to execute “cat /etc/issue” command in the remote server osetc.com, you can type the following command:

# ssh osetc.com "cat /etc/issue"

Outputs:

[root@osetc_test rar]# ssh osetc.com "cat /etc/issue"
\S
Kernel \r on an \m

After your ssh command exits, you can check the value of $? to get the exit code. type:

# echo $?

Outputs:

[root@osetc_test rar]# echo $?
0

SSH Return Codes


The below list is all returned codes of SSH Command:

0- Operation was successful
1 -Generic error, usually because invalid command line options or malformed configuration
2- Connection failed
65- Host not allowed to connect
66 -General error in ssh protocol
67 -Key exchange failed
68 -Reserved
69 -MAC error
70 -Compression error
71 -Service not available
72 -Protocol version not supported
73 -Host key not verifiable
74 -Connection failed
75 -Disconnected by application
76 -Too many connections
77 -Authentication cancelled by user
78 -No more authentication methods available
79 -Invalid user name