scripts/aslookup

77 lines
1.8 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 Net::Whois::RIPE;
sub output_result {
my ($target, %result) = @_;
if($target =~ /^AS/i) {
print "$target is ", $result{'descr'}, " (", $result{'as-name'}, ")\n";
} else {
my $as_descr = get_AS_descr($result{'origin'});
my $routename = "route";
$routename = "route6" unless defined $result{'route'};
print "$target (", $result{$routename}, " - ", $result{'descr'}, ") is advertised by ", $result{'origin'}, " $as_descr", "\n";
}
}
sub handle_whois_output {
my ($target,$obj) = @_;
while($obj->isnt_exhausted) {
my %wresult;
my @whoisdata = split /\n/, $obj->value();
foreach (@whoisdata) {
my $key = $_;
$key =~ s/^([a-z\-0-9]+):.*/$1/;
my $data = $_;
$data =~ s/^.*:[ ]+(.*)/$1/;
$wresult{$key} = $data;
}
output_result($target,%wresult);
}
}
sub get_AS_descr {
my $target = shift;
my $tw = do_whois($target);
my @whoisdata = split /\n/, $tw->value();
my ($descr, $asname);
foreach (@whoisdata) {
if(/^as-name:/) {
$asname = $_;
$asname =~ s/^.*:[ ]+(.*)/$1/;
}
if(/^descr:/) {
$descr = $_;
$descr =~ s/^.*:[ ]+(.*)/$1/;
last;
}
}
return "$asname - $descr";
}
sub do_whois {
my $target = shift;
my $default_as_server = "whois.radb.net";
my %whois_options = (
"hostname" => $default_as_server,
);
my $w = Net::Whois::RIPE->new(%whois_options);
return $w->query($target);
}
my $default_as_server = "whois.radb.net";
my %whois_options = (
"hostname" => $default_as_server,
);
if($#ARGV < 0 || $ARGV[0] eq "") {
print "Provide an IP or ASN to look up";
exit;
}
my $target = $ARGV[0];
my $whois = Net::Whois::RIPE->new(%whois_options);
my $witerator = $whois->query($target);
handle_whois_output($target, $witerator);