This script is used to ping a number of host IP’s and send an email if the host IP is down : You can add any number of IP’s under the ‘HOSTS’ variable.This is using the mail utility tool to sent the email alerts . As this is system generated emails, most of the mail domains will move it straight to the Spam folder. You might need to check the spam folder , find the mail and whitelist it . Also if you are using this on a corporate enviornment, most of the company domain spam filters will see it as a spam
#!/bin/bash
HOSTS="192.168.20.1 172.20.30.1 "
for myHost in $HOSTS;
do
ping -q -c 3 $myHost > /dev/null
if [ ! $? -eq 0 ]
then
echo "$(date) Host: $myHost is unresponsive (ping failed)" >> /home/monitor.txt
fi
done
#Email VARIABLES
SUBJECT="[ALERT] Host(s) Unresponsive!"
EMAILID="test@gmail.com"
#To Send email
if [ -e /home/monitor.txt ];
then
echo "$(cat /home/monitor.txt)" | mail -s "$SUBJECT $(date)" $EMAILID
rm -rf /usr/local/bin/monitor/down_now.txt
else
exit 0
fi
exit 0