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
}

Leave a Reply

Please log in using one of these methods to post your comment:

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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.