#!/bin/ksh # pipe ping output to this script. # ie: # ping www.google.com | ./ping-dropcount # # The output will be in the form of: # t= # and optionally, if packets were dropped: # t= (lost X) # where X is the number of packets lost LASTCOUNT=-1 while [ 1 ]; do read LINE case "$LINE" in 64*) LINE=`echo -n "${LINE}" | awk '{print $5" "$6" "$7" "$8}'` COUNT=`echo -n ${LINE} | perl -e '$_ = <>;s/.*icmp_seq=([0-9]+).*/$1/; print $_'` #LOSTPACKETS=`expr ${COUNT} - ${LASTCOUNT} - 1` LOSTPACKETS=$((${COUNT} - ${LASTCOUNT} - 1)) LINE=`echo -n "${LINE}" | awk '{print $3}'` LINE=${LINE#time=} echo -n "t=${LINE}" [ ${LOSTPACKETS} -gt 0 ] && echo -n " (lost ${LOSTPACKETS})" echo LASTCOUNT=${COUNT} ;; *) #echo "Ignored: ${LINE}" ;; esac done