Shell Script to Check Availability of Server using ping command. To check connectivity between servers is ok or not, which actually helps a lot especially if you wanted to know the stability of network connectivity.
[root@ranjeet]# vi /opt/script/pingutility.sh
#!/bin/bash
# Simple SHELL script for Linux and UNIX system monitoring with
# ping command
# -------------------------------------------------------------------------
# add ip / host name separated by white space , so that iterate further while ping
HOSTS="10.72.179.204 10.72.179.205 search.mysoftkey.com"
# number of ping requests
COUNT=1
LOG_FILE=/opt/ping-log.txt
while [ true ]
do
for myHost in $HOSTS
do
grepPing=$(ping -c $COUNT $myHost | grep 'time=' )
if [ "$myHost" == "search.mysoftkey.com" ];
then
timeUnit=$(echo $grepPing | awk '{ print $9 }' )
timetaken=$(echo $grepPing | awk '{ print $8 }' | awk -F'=' '{ print $2 }' | awk -F'.' '{ print $1 }' | awk '{printf( "%d", $1 )}')
else
timeUnit=$(echo $grepPing | awk '{ print $8 }' )
timetaken=$(echo $grepPing | awk '{ print $7 }' | awk -F'=' '{ print $2 }' | awk -F'.' '{ print $1 }' | awk '{printf( "%d", $1 )}')
fi
if [ "$timeUnit" != "" ];
then
if [ $timeUnit == "ms" -a $timetaken -lt 1 ];
then
echo "$grepPing, $myHost is online" `date`
else
echo "$grepPing, Host : $myHost is down (ping failed) at $(date)" >> $LOG_FILE
# echo "$grepPing, Host : $myHost is down (ping failed) at $(date)"
fi
else
echo "ping: unknown host $myHost, at $(date)" >> $LOG_FILE
fi
done
sleep 1
done
Run it in background Thread
Run the above script in background and leave it , when you come next day to office you can check /opt/ping-log.txt file what logged into or what not. Which can help you to identify somewhere.
sh /opt/script/pingutility.sh &
Output
You should see the output of above script
[root@ranjeet tmp]# tail -f /opt/ping-log.txt 64 bytes from 10.72.179.209: icmp_seq=1 ttl=127 time=1.06 ms, Host : 10.72.179.209 is down (ping failed) at Thu Aug 25 16:33:35 IST 2016 64 bytes from 10.72.179.209: icmp_seq=1 ttl=127 time=5.88 ms, Host : 10.72.179.209 is down (ping failed) at Thu Aug 25 16:33:44 IST 2016 64 bytes from 10.72.179.209: icmp_seq=1 ttl=127 time=7.09 ms, Host : 10.72.179.209 is down (ping failed) at Thu Aug 25 16:33:47 IST 2016 64 bytes from 10.72.179.209: icmp_seq=1 ttl=127 time=13.3 ms, Host : 10.72.179.209 is down (ping failed) at Thu Aug 25 16:33:52 IST 2016
Write your suggestion / comments to improve this post about how to check servers connectivity usng Linux shell script.