Linux/Unix Ping State Script

Posted: April 21, 2011 in CLI, DIY, Linux

Hello Guys,

After a good afternoon of work, I bring you a small script to check on server network status.
You know “Keep Alive”  style.
This small script is intended to review on how many responses do you get from a ping test.
and you can use the out put for what ever you like, like an php report.

This is the script code:

root@bobafett:/# cat /pingok.sh
#!/bin/bash
HOST=$1
COUNT=4
if [ $# -eq 0 ]; then
        HOST=localhost
fi
count=$(ping -c $COUNT $HOST 2> /dev/null | grep 'received' | awk -F',' '{print $2 }' | awk '{ print $1 }')
if [ -z $count ]; then
        echo "Invalid Output"
        exit 1
fi
#echo $HOST #THIS IS DEBUGING
if [ $count -eq 4 ]; then
        # 100% Worked
        echo "UP"
fi
if [ $count -eq 3 ]; then
        # 25% failed
        echo "H/LOAD"
fi
if [ $count -eq 2 ]; then
        # 50% failed
        echo "INTERMIT"
fi
if [ $count -eq 1 ]; then
        # 75% failed
        echo "DEPRECATED"
fi
if [ $count -eq 0 ]; then
        # 100% failed
        echo "DOWN"
fi

This is a screenshot of how it looks in a PHP report.

I cant copy the entire PHP code, but I will be more than glad to share if you want it.

PHP Report Example

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s