#!/usr/bin/perl # # Name: ip-as-geo.pl # Version: 1.0.5 # Date: 2008-08-31 # Author: Jim Clausing # Inputs: IP address # Outputs: IP # CIDR range(s) assigned to # 2-letter country code where IP located # long country name where IP is located # AS number # BGP prefix # Organization AS assigned to # use strict; use warnings; use Getopt::Std; use Net::Abuse::Utils qw( :all ); use Net::Whois::IANA qw(whois_query inetnum); #use Net::CIDR; use Geography::Countries qw (country); my %opt; getopts('hHn',\%opt) || &usage(); &usage() if $opt{'h'}; my $ip = $ARGV[0]; my $iana = new Net::Whois::IANA; if (!is_ip($ip)) { warn "$ip doesn't look like an IP.\n"; exit; } $iana->whois_query(-ip=>$ip); my @asn = get_asn_info($ip); my $asn_desc = get_as_description($asn[0]) || ''; #my $cidr = $iana->cidr(); # # for some reason $iana->cidr() wasn't returning the CIDR, so we'll do it ourselves # my $net = $iana->inetnum(); my @cidr = Net::CIDR::range2cidr($net) if !$opt{'n'}; my $cidr_out; if ( $opt{'n'} ) { $cidr_out = $net; } elsif ( $#cidr == 0 ) { $cidr_out = $cidr[0]; } elsif ( $#cidr == 2 ) { $cidr_out = join(',',@cidr); } else { # it just looks too crowded if the netrange requires 3 or more CIDR blocks to describe $cidr_out = $net; } my $country = get_ip_country($ip); my $country_long = country($country); if ( !$opt{'H'} ) { print "$ip|$cidr_out|$country|$country_long|$asn[0]|$asn[1]|$asn_desc\n"; } else { format = IP addr | CIDR/inetnum |CC|Country (long)| ASN | BGP Prefix | Owner @|||||||||||||| | @|||||||||||||||||||||||||||||||| |@<|@<<<<<<<<<<<<<|@>>>>|@||||||||||||||||| |@<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $ip, $cidr_out, $country, $country_long, $asn[0], $asn[1], $asn_desc . write; } exit 0; sub usage() { print "$0 [-h][-n] -h print this message -n print IP range, rather than CIDR(s) -H print headers for each column "; exit; }