Improved ip2asn (BGP Ranking, host lookup added)

This commit is contained in:
Alexandre Dulaunoy 2011-05-03 15:48:44 +02:00
parent 9adbdacd06
commit 45a6c81140

View file

@ -2,31 +2,62 @@
# #
# Takes as input IP address (one per line) # Takes as input IP address (one per line)
# and output the guessed IP location along with ASN origin and its description # and output the guessed IP location along with ASN origin and its description
# and the BGP Ranking of each ASN
# #
# perl iporigin.pl #perl ip2asn.pl
# www.microsoft.com
# US;AS8075;MICROSOFT-CORP---MSN-AS-BLOCK - Microsoft Corp;65.55.12.249;8075,1.00036643769349,3/9
# 8.8.8.8 # 8.8.8.8
# US;AS15169;GOOGLE - Google Inc.;8.8.8.8 # US;AS15169;GOOGLE - Google Inc.;8.8.8.8;15169,1.00210796734234,4/9
# 8.8.4.4 # www.google.com
# US;AS15169;GOOGLE - Google Inc.;8.8.4.4 # US;AS15169;GOOGLE - Google Inc.;74.125.230.84;15169,1.00210796734234,4/9
# 4.4.4.4 # 4.4.4.4
# US;AS3356;LEVEL3 Level 3 Communications;4.4.4.4 # US;AS3356;LEVEL3 Level 3 Communications;4.4.4.4;3356,1.00000229260952,4/9
# #
# #
# This file is in the public domain. # This file is in the public domain.
# #
# Alexandre Dulaunoy - http://github.com/adulau # Alexandre Dulaunoy - http://github.com/adulau
use strict;
use Net::Whois::RIS; use Net::Whois::RIS;
use IP::Country::Fast; use IP::Country::Fast;
use Socket;
my $country = IP::Country::Fast->new(); my $country = IP::Country::Fast->new();
$| = 1; $| = 1;
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();
}
while (<STDIN>) { while (<STDIN>) {
next if /^#/; next if /^#/;
chomp(); chomp();
@v = split(/ /, $_); my @v = split( / /, $_ );
$v[0]; if ( !( $v[0] =~ /^(\d+\.){3}\d+$/ ) ) {
$l = Net::Whois::RIS->new(); my $ipn = inet_aton( $v[0] ) or next;
$l->getIPInfo($v[0]); $v[0] = inet_ntoa($ipn);
print $country->inet_atocc($v[0]).";".$l->getOrigin().";".$l->getDescr().";".$v[0]."\n"; }
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";
} }