shell | scripting ping


Theres a little problem using ping as a connection checker inside of scripts. In example: If you send a bulk of three pings and one of them fail, the ping coommand returns 1.
Here is a workaround:

function ping_wrapper() {
C_IP="$1"
C_CNT="$2"
C_TIMEOUT="$3"
CNT=0
while true; do
ping -q -c 1 -w $C_TIMEOUT $C_IP > /dev/null && break
CNT=$(($CNT + 1))
if [ $CNT -eq $C_CNT ]; then return 1; fi
done
return 0
}

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