2014-02-02 08:03:08 +00:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
#aslookup.pl - Looks up information on a given ASN or IP address
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
2014-02-02 08:32:53 +00:00
|
|
|
use diagnostics;
|
2014-02-02 08:03:08 +00:00
|
|
|
|
|
|
|
use Net::IRR;
|
|
|
|
|
|
|
|
my $default_as_server = "whois.radb.net";
|
|
|
|
my $as_server = $default_as_server;
|
|
|
|
our $db = Net::IRR->connect(host=>$as_server) or die "Error: Cannot connect to whois server $as_server:43";
|
|
|
|
|
|
|
|
sub do_as_lookup {
|
|
|
|
my $asn = shift;
|
2014-02-02 08:32:53 +00:00
|
|
|
if($asn =~ / /) {
|
|
|
|
my @asns = split / /, $asn;
|
|
|
|
my $ret = "";
|
|
|
|
foreach(@asns) {
|
|
|
|
$ret .= do_as_lookup("$_").", ";
|
|
|
|
}
|
|
|
|
$ret =~ s/, $//g;
|
|
|
|
return $ret;
|
|
|
|
}
|
2014-02-02 08:03:08 +00:00
|
|
|
my @results = $db->match("aut-num",$asn) or return "$asn - unknown AS";
|
|
|
|
@results = split /\n/,$results[0];
|
|
|
|
my $asname = "";
|
|
|
|
my $asdesc = "";
|
|
|
|
foreach(@results) {
|
|
|
|
$asname = $1 if /as-name:\s+(.+)$/;
|
2014-03-05 01:19:03 +00:00
|
|
|
$asdesc .= ", " if length $asdesc > 0 and /^descr/;
|
|
|
|
$asdesc .= $1 if /descr:\s+(.+)$/;
|
2014-02-02 08:03:08 +00:00
|
|
|
}
|
2014-02-02 08:32:53 +00:00
|
|
|
return "$asn $asname - $asdesc";
|
2014-02-02 08:03:08 +00:00
|
|
|
}
|
|
|
|
sub do_subnet_lookup {
|
|
|
|
my $sub = shift;
|
|
|
|
my $result;
|
|
|
|
$result = $db->route_search($sub,Net::IRR::ONE_LEVEL) or die "Error: Couldn't seem to get a result for $sub.";
|
|
|
|
my @res = split /\n/, $result;
|
|
|
|
$result = "";
|
|
|
|
my $descr = "";
|
|
|
|
my $route = "";
|
|
|
|
foreach(@res) {
|
2014-02-02 08:32:53 +00:00
|
|
|
$route = $1 if /route6?:\s+(.+)$/;
|
2014-02-02 08:03:08 +00:00
|
|
|
$result .= $1." " if /origin:\s+(AS[0-9]+)$/;
|
|
|
|
$descr .= $1." : " if /descr:\s+(.+)$/;
|
|
|
|
}
|
|
|
|
$result =~ s/ +$//g;
|
|
|
|
$descr =~ s/ : $//g;
|
|
|
|
$sub = $route unless $db->route_search($sub,Net::IRR::EXACT_MATCH);
|
|
|
|
return "$sub€$result€$descr";
|
|
|
|
}
|
|
|
|
|
2014-03-05 01:19:03 +00:00
|
|
|
my $target = uc shift || die "Please provide an ASnum or IP/subnet to look up\n";
|
2014-02-02 08:03:08 +00:00
|
|
|
if($target =~ /^AS/) {
|
|
|
|
print "$target is ".do_as_lookup $target;print "\n";
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
my ($s,$a,$d) = split /€/, do_subnet_lookup $target;
|
|
|
|
print "$s, $d";
|
|
|
|
print " (contains $target)" unless $target eq $s;
|
2014-02-02 08:32:53 +00:00
|
|
|
print " is announced by ".do_as_lookup $a;
|
2014-02-02 08:03:08 +00:00
|
|
|
print "\n";
|
|
|
|
$db->disconnect;
|