从系统的traceroute命令获取路径,并解析出路径中每一跳IP的所在地
Usage: traceroutex YOURIP
#!/usr/bin/perl -w
use strict;
use LWP;
my $IP = shift;
my $LIP;
my $Locate;
open(TRACERT,"/bin/traceroute $IP |");
while(<TRACERT>){
next if m/^trace/;
($LIP) = m/\((.+?)\)/;
$Locate = locate($LIP);
print "$LIP\t\t$Locate\n";
}
sub locate {
my $ip = shift;
my ($content,$message,$is_success) = do_POST(
'',
['ip' => "$ip",'action' => 2]
);
die "Error in locat\(\) : $message" unless $is_success;
if($content =~ m{参考数据二:(.+?)</li>}) {
return $1;
} else {
die "Can't locat address: $ip\n";
}
}
sub do_POST {
my $browser = LWP::UserAgent->new( );
my $resp = $browser->post(@_);
return ($resp->content, $resp->status_line, $resp->is_success, $resp)
if wantarray;
return unless $resp->is_success;
return $resp->content;
}
|
阅读(3538) | 评论(0) | 转发(0) |