linux poison RSS
linux poison Email

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




3 comments:

nud3lz said...

Perhaps a check if the server.txt file is existent? something like:


SERVERS=server.txt

if [ ! -f $SERVERS ];
then
echo "$SERVERS not found"
else
....place above code here...
fi

Anonymous said...

You might want to check out fping - which will do exactly this - pass the hosts via the command line or via a file

yourhitesh said...

How can i modify this script to Ping different servers from different servers, like the script which telnet and login into a 2nd server and check for the same of different ping status and then login(telnet) into 3rd server and pings some other 3-4 servers??? pls help

Post a Comment

Related Posts with Thumbnails