From 45a6c81140ceb1d2c16d40e7be97a883ff9463f6 Mon Sep 17 00:00:00 2001 From: Alexandre Dulaunoy Date: Tue, 3 May 2011 15:48:44 +0200 Subject: [PATCH] Improved ip2asn (BGP Ranking, host lookup added) --- bin/ip2asn.pl | 63 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 47 insertions(+), 16 deletions(-) diff --git a/bin/ip2asn.pl b/bin/ip2asn.pl index 69a979b..2796cdc 100644 --- a/bin/ip2asn.pl +++ b/bin/ip2asn.pl @@ -2,31 +2,62 @@ # # Takes as input IP address (one per line) # 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 -# US;AS15169;GOOGLE - Google Inc.;8.8.8.8 -# 8.8.4.4 -# US;AS15169;GOOGLE - Google Inc.;8.8.4.4 +# 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 # 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. -# +# # Alexandre Dulaunoy - http://github.com/adulau +use strict; + use Net::Whois::RIS; -use IP::Country::Fast; +use IP::Country::Fast; +use Socket; my $country = IP::Country::Fast->new(); $| = 1; -while () { - next if /^#/; - chomp(); - @v = split(/ /, $_); - $v[0]; - $l = Net::Whois::RIS->new(); - $l->getIPInfo($v[0]); - print $country->inet_atocc($v[0]).";".$l->getOrigin().";".$l->getDescr().";".$v[0]."\n"; +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 () { + 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"; }