scripts/aslookup

81 lines
1.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env perl
#aslookup.pl - Looks up information on a given ASN or IP address
use strict;
use warnings;
use Net::Whois::RIPE;
2013-03-17 17:56:37 +00:00
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";
}
}
2013-03-17 05:58:59 +00:00
sub handle_whois_output {
2013-03-17 17:56:37 +00:00
my ($target,$obj) = @_;
2013-03-17 05:58:59 +00:00
while($obj->isnt_exhausted) {
2013-03-17 17:56:37 +00:00
my %wresult;
2013-03-17 05:58:59 +00:00
my @whoisdata = split /\n/, $obj->value();
if($whoisdata[0] =~ /^%/) {
print "Lookup for $target failed!\n";
return;
}
2013-03-17 05:58:59 +00:00
foreach (@whoisdata) {
2013-03-17 17:56:37 +00:00
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/;
2013-03-17 05:58:59 +00:00
}
2013-03-17 17:56:37 +00:00
if(/^descr:/) {
$descr = $_;
$descr =~ s/^.*:[ ]+(.*)/$1/;
last;
2013-03-17 05:58:59 +00:00
}
}
2013-03-17 17:56:37 +00:00
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);
2013-03-17 05:58:59 +00:00
}
my $default_as_server = "whois.radb.net";
my %whois_options = (
"hostname" => $default_as_server,
);
2013-03-17 05:39:51 +00:00
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);
2013-03-17 05:37:32 +00:00
my $witerator = $whois->query($target);
2013-03-17 17:56:37 +00:00
handle_whois_output($target, $witerator);