Ping is a program used to detect a host's reachability on a network. Probably it's executing from the command line followed by hostname or IP address. This ping command is utilizing the ICMP protocol, the target host is sent an ECHO_REQUEST packet and responds with an ECHO_RESPONSE if available.
If you would like to be checked the ping status by the scripting, ping the multiple destination IP addresses for every second. This script can check the ping status to all the IP's which is you have included in hostname.txt and send E-mail alerts If any one of the server ping status is down.
Create a file with the name of ping_status.sh and give execute permission, then create an another file name hostname.txt and add your IP addresses or host name.
prerequisites:
You have to install the below packages,
1. Open-ssh
2. Mutt,
3. Postfix (or) Exim
#!/bin/bash
SetParamRun() {
export TIME_HTTP=`date +%F_%T:%3N`
export STATUS_DOWN_HTTP=`echo -e "\E[31m[ DOWN ]\E[0m"`
export STATUS_UP_HTTP=`echo -e "\E[32m[ RUNNING ]\E[0m"`
}
Ping_Hosts() {
SetParamRun
cat hostname.txt | while read next
do
server=`echo $next | awk -F":" '{print $1}'`
ping -i 1 -c 1 $server > /dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "$TIME_HTTP : Status Of Host $server = $STATUS_UP_HTTP";
else
echo "$TIME_HTTP : Status Of Host $server = $STATUS_DOWN_HTTP";
echo "$TIME_HTTP : Status Of Host $server = $STATUS_DOWN_HTTP" | mutt -s "$server Host DOWN!!!" youremail@domain.com
fi
done;
}
i=1
while [ $i -le 2 ]
do
SetParamRun
sleep 1s
Ping_Hosts | tee -a /var/log/ping.log
done
We might give execute permission,
$ chmod +x check_ping.sh
$sh check_ping.sh
Comments (0)