Last active
January 23, 2017 11:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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