Here is the simple bash script to check the server availability using simple ping command, for this you need to create a simple txt file ("server.txt") containing the hostname or ip address of the servers that you want to check ...
yahoo.com
redhat.com
# Read the file line by line
cat server.txt | while read line
do
# check if there are no blank lines
if [ ! -z $line ]; then
PINGCOUNT=2
PING=$(ping -c $PINGCOUNT $line | grep received | cut -d ',' -f2 | cut -d ' ' -f2)
if [ $PING -eq 0 ]; then
echo "Something wrong with the server: $line"
# Or do send out mail
else
echo "All good: $line"
fi
fi
done
All good: yahoo.com
All good: redhat.com
$ cat server.txt
google.comyahoo.com
redhat.com
$ cat pingalert.sh
#!/bin/bash# Read the file line by line
cat server.txt | while read line
do
# check if there are no blank lines
if [ ! -z $line ]; then
PINGCOUNT=2
PING=$(ping -c $PINGCOUNT $line | grep received | cut -d ',' -f2 | cut -d ' ' -f2)
if [ $PING -eq 0 ]; then
echo "Something wrong with the server: $line"
# Or do send out mail
else
echo "All good: $line"
fi
fi
done
Output: $ ./pingalert.sh
All good: google.comAll good: yahoo.com
All good: redhat.com
source:http://linuxpoison.blogspot.com/2012/05/135781677513232.html