v1
This commit is contained in:
commit
fa3bdf5590
66
xtr.pl
Normal file
66
xtr.pl
Normal file
@ -0,0 +1,66 @@
|
||||
#!/usr/bin/perl
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Net::Traceroute;
|
||||
use Net::DNS::Resolver;
|
||||
use Dancer2;
|
||||
#use JSON::PP;
|
||||
use Data::Dumper;
|
||||
|
||||
|
||||
hook 'before' => sub {
|
||||
header 'Access-Control-Allow-Origin' => '*';
|
||||
};
|
||||
|
||||
get '/:ip' => sub {
|
||||
my $ip = route_parameters->get('ip') || 8.8.8.8;
|
||||
my $trace = traceit($ip);
|
||||
return encode_json $trace;
|
||||
};
|
||||
|
||||
start;
|
||||
|
||||
|
||||
|
||||
|
||||
sub traceit
|
||||
{
|
||||
my ($host) = @_;
|
||||
my $tr = Net::Traceroute->new(host => $host, use_icmp => 1, timeout => 1 );
|
||||
|
||||
my @array = ();
|
||||
if($tr->found)
|
||||
{
|
||||
#print Dumper($tr->hops);
|
||||
foreach my $hop (@{$tr->{'hops'}})
|
||||
{
|
||||
my $sum = sprintf("%.1f",($hop->[0]->[2] + $hop->[1]->[2] + $hop->[2]->[2])/3);
|
||||
my $res = Net::DNS::Resolver->new;
|
||||
my $query = $res->query($hop->[0]->[1], 'PTR');
|
||||
my $answer = "nonono";
|
||||
if($query)
|
||||
{
|
||||
foreach my $rr ($query->answer)
|
||||
{
|
||||
if($rr->type eq "PTR")
|
||||
{
|
||||
$answer = $rr->ptrdname;
|
||||
}
|
||||
}
|
||||
}
|
||||
# print $hop->[0]->[1]."\t(".$answer.") - ".$sum."\n";
|
||||
push(@array, [$hop->[0]->[1],$answer,$sum]);
|
||||
}
|
||||
}
|
||||
|
||||
if($tr->found)
|
||||
{
|
||||
return \@array;
|
||||
}
|
||||
else
|
||||
{
|
||||
return $tr->{'hops'};
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user