2011-04-03 10:11:39 +00:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
|
|
|
# Takes as input IP address (one per line)
|
|
|
|
# and output the guessed IP location along with ASN origin and its description
|
2011-05-03 13:48:44 +00:00
|
|
|
# and the BGP Ranking of each ASN
|
2011-04-03 10:11:39 +00:00
|
|
|
#
|
2011-05-03 13:48:44 +00:00
|
|
|
#perl ip2asn.pl
|
|
|
|
# www.microsoft.com
|
|
|
|
# US;AS8075;MICROSOFT-CORP---MSN-AS-BLOCK - Microsoft Corp;65.55.12.249;8075,1.00036643769349,3/9
|
2011-04-03 10:11:39 +00:00
|
|
|
# 8.8.8.8
|
2011-05-03 13:48:44 +00:00
|
|
|
# US;AS15169;GOOGLE - Google Inc.;8.8.8.8;15169,1.00210796734234,4/9
|
|
|
|
# www.google.com
|
|
|
|
# US;AS15169;GOOGLE - Google Inc.;74.125.230.84;15169,1.00210796734234,4/9
|
2011-04-03 10:11:39 +00:00
|
|
|
# 4.4.4.4
|
2011-05-03 13:48:44 +00:00
|
|
|
# US;AS3356;LEVEL3 Level 3 Communications;4.4.4.4;3356,1.00000229260952,4/9
|
|
|
|
#
|
2011-04-03 10:11:39 +00:00
|
|
|
#
|
|
|
|
# This file is in the public domain.
|
2011-05-03 13:48:44 +00:00
|
|
|
#
|
2011-04-03 10:11:39 +00:00
|
|
|
# Alexandre Dulaunoy - http://github.com/adulau
|
|
|
|
|
2011-05-03 13:48:44 +00:00
|
|
|
use strict;
|
|
|
|
|
2011-04-03 10:11:39 +00:00
|
|
|
use Net::Whois::RIS;
|
2011-05-03 13:48:44 +00:00
|
|
|
use IP::Country::Fast;
|
|
|
|
use Socket;
|
2011-04-03 10:11:39 +00:00
|
|
|
my $country = IP::Country::Fast->new();
|
|
|
|
$| = 1;
|
|
|
|
|
2011-05-03 13:48:44 +00:00
|
|
|
sub BGPRankingLookup {
|
|
|
|
my $asn = shift;
|
|
|
|
$asn =~ s/AS//g;
|
|
|
|
|
|
|
|
my $bgpranking =
|
|
|
|
IO::Socket::INET->new( PeerAddr => "pdns.circl.lu", PeerPort => 43 )
|
|
|
|
or die();
|
|
|
|
print $bgpranking $asn . "\n";
|
|
|
|
my $x;
|
|
|
|
while (<$bgpranking>) {
|
|
|
|
$x = $x . $_;
|
|
|
|
}
|
|
|
|
chomp($x);
|
|
|
|
return $x;
|
|
|
|
|
|
|
|
$bgpranking->shutdown();
|
|
|
|
}
|
2011-04-03 10:11:39 +00:00
|
|
|
while (<STDIN>) {
|
2011-05-03 13:48:44 +00:00
|
|
|
next if /^#/;
|
|
|
|
chomp();
|
|
|
|
my @v = split( / /, $_ );
|
|
|
|
if ( !( $v[0] =~ /^(\d+\.){3}\d+$/ ) ) {
|
|
|
|
my $ipn = inet_aton( $v[0] ) or next;
|
|
|
|
$v[0] = inet_ntoa($ipn);
|
|
|
|
}
|
|
|
|
my $l = Net::Whois::RIS->new();
|
|
|
|
$l->getIPInfo( $v[0] );
|
|
|
|
my $origin = $l->getOrigin();
|
|
|
|
print $country->inet_atocc( $v[0] ) . ";"
|
|
|
|
. $origin . ";"
|
|
|
|
. $l->getDescr() . ";"
|
|
|
|
. $v[0] . ";"
|
|
|
|
. BGPRankingLookup($origin) . "\n";
|
2011-04-03 10:11:39 +00:00
|
|
|
}
|