Quickly check hosts with ping

I needed a script for a quick health check of a bunch of servers.
This is how I did it using the ping command:

for((i=1;i<42;i++)); do
    ping -c 1 -W 3 host${i}.domain.com &> /dev/null
    if [ $? -ne 0 ] ; then
        echo "host${i} is down"
    else
        echo "host${i} is up"
    fi
done

You can also use netcat and check a specific port:

netcat -z -w 2  host${i}.domain.com 80 &> /dev/null

This entry was written by CharlyBr, posted on August 14, 2009 at 1:46 pm, filed under Command line and tagged , , . Leave a comment or view the discussion at the permalink.