Shell script to check whether a server is reachable?

The most barebones check you can do is probably to use netcat to check for open ports.

to check for SSH (port 22) reachability, you can do

if nc -z $server 22 2>/dev/null; then
    echo "$server ✓"
else
    echo "$server ✗"
fi

from the manpage:

-z   Specifies that nc should just scan for listening daemons, without sending any data to them.


Your ssh command will test for more than if the server is reachable - for it to work, the ssh server must be running, and everything must be right with authentication.

To just see if the servers are up, how about just a simple ping?

ping -c1 -W1 $ip_addr && echo 'server is up' || echo 'server is down'