Monday, March 18, 2013

My take on pathping for Linux

Today I was wondering if there was a ported version of Microsoft's tool pathping. As i couldn't find nothing similar, I decided to write a quick script in perl with similar basic functions during my lunch break - lots of features are missing but I might be adding them the following days.

You will need Net::Traceroute and Net::Ping modules -> cpan -i Net::Traceroute Net::Ping

#!/usr/bin/perl
use Net::Traceroute;
use Net::Ping;

if (@ARGV <1) { 
        die "usage: pathping.pl \<host\>";
        }
print "Host: " . $ARGV[0] . " --> " ;

$totalpings = "10";
$pingtimeout = "2";
$tr = Net::Traceroute->new(host => $ARGV[0]);
if ( $tr->stat == '0' ) {
        my $hops = $tr->hops;
        my $distance = $tr->hop_query_time(($hops,TRACEROUTE_OK));
        if($hops > 1) {
            print "Hops: " . $hops . " Distance: " . $distance . "ms \n\n";
            for ($count=1; $count <= $hops; $count++) {
                print "Hop $count: ";
                $currenthop = $tr->hop_query_host($count,TRACEROUTE_OK);
                $currenthoptime = $tr->hop_query_time(($count,TRACEROUTE_OK));
                if ( ! $currenthop) { $currenthop = "*"; }
                print "$currenthop  $currenthoptime ms [";
                $loss=0;
                for ($lossloop=1; $lossloop <=$totalpings; $lossloop++) {

                        $p = Net::Ping->new("icmp",$pingtimeout);
                        if ($p->ping($currenthop)) {
                        print "!";
                        }
                        else {
                        $loss++;
                        print ".";
                        }
                        $p->close();
                        }
                print "] Packet loss: " . $loss . "/" . $totalpings . " " . ($loss*100)/$totalpings . "%";
                print "\n";
                }

    }

}
else {
        print "Host not found? Code: " . $tr->stat . "\n";
        }


Be aware that you will need sudo or to be root in order to run this script. This is the output:



$ sudo perl pathping.pl www.av.com

Host: www.av.com --> Hops: 12 Distance: 15.288ms

Hop 1: 10.65.17.2  0.91 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 2: 10.65.16.252  2.322 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 3: 165.21.240.189  12.167 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 4: 165.21.12.68  22.66 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 5: 203.208.190.21  12.139 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 6: 203.208.151.157  12.367 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 7: 203.84.209.229  11.467 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 8: 203.84.209.89  14.301 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 9: 106.10.128.7  12.77 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 10: 106.10.128.25  13.527 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 11: 106.10.128.213  14.378 ms [!!!!!!!!!!] Packet loss: 0/10 0%
Hop 12: 106.10.165.51  15.288 ms [!!!!!!!!!!] Packet loss: 0/10 0%





1 comment: