Skip to content

Instantly share code, notes, and snippets.

@13Cubed
Last active January 23, 2017 11:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 13Cubed/d87759623b1be50d3cf7 to your computer and use it in GitHub Desktop.
Save 13Cubed/d87759623b1be50d3cf7 to your computer and use it in GitHub Desktop.
A simple Bash script to monitor a remote address and send an email when it goes down.
#!/bin/bash
# If the file that holds the flag doesn't exist, create it with default of 0
if [ ! -f /tmp/checknet.tmp ]
then
echo 0 > /tmp/checknet.tmp
fi
target=TARGET_GOES_HERE
email=EMAIL_GOES_HERE
flag=$(cat /tmp/checknet.tmp)
timestamp=$(date "+%d-%b-%Y %H:%M")
ping -c 1 ${target}
# If the ping fails ...
if [ $? -ne 0 ]
then
if [ "$flag" -eq "0" ]
then
echo "Ping to $target failed!" | mail -s "Connection Down ($timestamp)" $email
echo 1 > /tmp/checknet.tmp
fi
# If the ping is successful ...
else
if [ "$flag" -eq "1" ]
then
echo "Ping to $target successful!" | mail -s "Connection Up ($timestamp)" $email
echo 0 > /tmp/checknet.tmp
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment