#!/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) { my $out = "$target is ".$result{'descr'}; if(defined($result{'as-name'})) { $out .= " (".$result{'as-name'}.")"; } print "$out\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,$did_ripe_whois) = @_; while($obj->isnt_exhausted) { my %wresult; my @whoisdata = split /\n/, $obj->value(); if(!defined($whoisdata[0])) { next; } elsif($whoisdata[0] =~ /^%.*(no entries found)/i) { if($did_ripe_whois == 1 || $target !~ /^AS/i) { print "Lookup for $target failed! \n"; } else { handle_whois_output($target, do_ripe_whois($target), 1); } return; } elsif($whoisdata[0] =~ /^%/) { next; } 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); } sub do_ripe_whois { my $target = shift; my $w = Net::Whois::RIPE->new(); return $w->query($target); } my $default_as_server = "whois.radb.net"; if($#ARGV < 0 || $ARGV[0] eq "") { print "Provide an IP or ASN to look up"; exit; } my $target = $ARGV[0]; handle_whois_output($target, do_whois($target), 0);