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.

#!/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.

References

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