Bash script to check SSH connection

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.


ssh -q -o BatchMode=yes -o ConnectTimeout=10 exit

if [ $? -ne 0 ]
  # Do stuff here if SSH is down
  echo 'Can not connect to' | mail -s " down" whoever@wherever

If it can’t SSH within 10 seconds it’ll send an email to whoever@wherever.


Last modified: 19/06/2015 Tags: ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top