You can use the following bash script to check whether an SSH connection is working. I run it via a cron job so that I receive an email alert when there’s a problem with a server.
#!/bin/bash
ssh -q -o BatchMode=yes -o ConnectTimeout=10 example.com exit
if [ $? -ne 0 ]
then
# Do stuff here if example.com SSH is down
echo 'Can not connect to example.com' | mail -s "example.com down" whoever@wherever
fi
If it can’t SSH within 10 seconds it’ll send an email to whoever@wherever.