Page 1 of 1

automated IP WHOIS lookup

Posted: Wed Aug 27, 2008 3:05 pm
by m2
I needed WHOIS for a number if IPs to find out their country. CPAN has a great package called Net::Whois::IANA that automates the WHOIS lookup. Here's the PERL code to loop through a text file (file name is iplist.txt, 1 IP on each line) and print results. I only needed the country and regional WHOIS. There's other WHOIS data available in comments if you need that data. (The sleep command limits traffic to the regional servers so the script isn't blocked. You might have to increase the sleep time or run batches if you analyze a lot of IPs.)

Code: Select all

#!/usr/bin/perl -w
use strict;
use Net::Whois::IANA;
print "\n";
open IP, '<iplist.txt' || die "Can't open iplist.txt";
while (<IP>) {
  chomp;
  my $ip = $_;
  my $iana = new Net::Whois::IANA;
  $iana->whois_query(-ip=>$_);
  print $ip, ": Country: ", $iana->country(), ' (', $iana->server(), ")\n";
#  print "Netname: " . $iana->netname() . "\n";
#  print "Descr: "   . $iana->descr()   . "\n";
#  print "Status: "  . $iana->status()  . "\n";
#  print "Source: "  . $iana->source()  . "\n";
#  print "Server: "  . $iana->server()  . "\n";
#  print "Inetnum: " . $iana->inetnum() . "\n";
#  print "CIDR: "    . $iana->cidr()    . "\n";
  sleep 1;
}


m2