Average ping time

From EggeWiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

This perl one-liner sends ten 100 byte packets to hostname, and then displays the average.

<geshi lang="perl"> /usr/sbin/ping -s hostname 100 10 | perl -ne \ 'BEGIN{ $sum = 0.0; $count = 0} if (/time[=]([0-9]+.[0-9]+) ms/) { $sum += $1; $count++;} END{ $avg = $sum/$count; print "$avg\n"} ' </geshi>

This works with Solaris 10's ping, which outputs data in this format:

$ /usr/sbin/ping -s hostname 100 10
PING hostname : 100 data bytes
108 bytes from hostname (192.168.0.1): icmp_seq=0. time=0.607 ms
108 bytes from hostname (192.168.0.1): icmp_seq=1. time=0.500 ms
108 bytes from hostname (192.168.0.1): icmp_seq=2. time=0.500 ms