scripts/aslookup-new

65 lines
1.7 KiB
Perl
Executable File

#!/usr/bin/env perl
#aslookup.pl - Looks up information on a given ASN or IP address
use strict;
use warnings;
use diagnostics;
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;
if($asn =~ / /) {
my @asns = split / /, $asn;
my $ret = "";
foreach(@asns) {
$ret .= do_as_lookup("$_").", ";
}
$ret =~ s/, $//g;
return $ret;
}
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+(.+)$/;
$asdesc .= ", " if length $asdesc > 0 and /^descr/;
$asdesc .= $1 if /descr:\s+(.+)$/;
}
return "$asn $asname - $asdesc";
}
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) {
$route = $1 if /route6?:\s+(.+)$/;
$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";
}
my $target = uc shift || die "Please provide an ASnum or IP/subnet to look up\n";
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;
print " is announced by ".do_as_lookup $a;
print "\n";
$db->disconnect;