#!/usr/bin/perl $Target = $ARGV[0]; $PLCount = ($ARGV[1] <= 4) ? 4 : $ARGV[1]; $Ping = "ping -l 3 -c $PLCount -i .2 -w 2 $Target |"; open(PING, $Ping) || die "roundtrip:U packetloss:U\n"; while() { #64 bytes from 192.168.3.1: icmp_seq=3 ttl=254 time=16.0 ms if(/time=(\d+\.?\d*)\sms/) { if($1 eq "<") {push(@RTValues, 0)} else {push(@RTValues, $1)} $PLCount--; } if(/(\d+)\%\spacket\sloss/) { $PacketLoss = $1; } } close(PING); #Calculate the Average Round-Trip Time @RTValues = sort {$a <=> $b} @RTValues; #Sorts the numbers shift(@RTValues); #Removes the lowest number pop(@RTValues); #Removes the highest number while($i <= $#RTValues) { $Average += $RTValues[$i]; $i++; } $values = $#RTValues + 1; if ($values <= 0) { $values = 1; $PacketLoss = 100; } $Average = $Average / $values; $Average = sprintf("%.4f", $Average); #Round Off Decimals if($PacketLoss == 100 || $PLCount == 4) {$Average = "U"} print "roundtrip:$Average packetloss:$PacketLoss";