Wednesday, May 16, 2012
2:57 AM

Bash Script: Check Servers Availability using ping command

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 ...

$ cat server.txt
google.com
yahoo.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.com
All good: yahoo.com
All good: redhat.com


0 comments:

Post a Comment