Skip to content

Instantly share code, notes, and snippets.

@13Cubed 13Cubed/checknet.sh
Last active Jan 23, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.