I was hoping that you all would solve this, but no one has answered the question. He asked for a script that would do it, not information on where to find it. Is this a programmers site or what? sheesh...
Alright. To understand how the script works, you have to know something about networking. Specifically the process of mapping domain names to IP Addresses, cause that's where the magic happens.
Skipping the cool information on how your computer does lookups and how it does the domain name to IP and DNS, DHCP info we'll get to the meat of it. The IP mappings are all stored in a database by central registries. These are the ones who play a vital role in Domain Name mitigation. They are ARIN, RIPE, APNIC, LACNIC and recently IANA.
The reason why all the scripts look like they are querying other sites is because they are. They hold a copy of Internic's international registry for domain names and ip addresses. Internic doesn't allow querying beyond registrars and whois, but some of the others do.
Why would you query a domain name registry for an IP? Again, skipping the DHCP, DNS discussion, registries have a database that maps domain names, (like programmingtalk.com) to their IP address, (69.20.37.97) - <CMD: PING programmingtalk.com in case you all wanted to know how I did that.
In that database you will find alot of useful information, which we'll get to in a moment, but first, let's finish the process.
If you were going to write you own script, you first have to convert an ip address to an ip number. I'm a perl developer so...
IP Address -> IP Number
Code:
my (@octets,$octet,$ip_number,$number_convert,$ip_address);
$ip_address = $ARGV[0];
chomp ($ip_address);
@octets = split(/\./, $ip_address);
$ip_number = 0;
foreach $octet (@octets) {
$ip_number <<= 8;
$ip_number |= $octet;
}
print "\nThe IP Address $ip_address converts to the IP Number $ip_number.\n";
in PHP it would be:
Code:
$ip_number = ip2long($ip_address);
the reverse, ip number to ip address would be:
Code:
my (@octets,$i,$ip_number,$ip_number_display,$number_convert,$ip_address);
$ip_number_display = $ip_number = $ARGV[0];
chomp($ip_number);
for($i = 3; $i >= 0; $i--) {
$octets[$i] = ($ip_number & 0xFF);
$ip_number >>= 8;
}
$ip_address = join('.', @octets);
print "\nThe IP Number $ip_number_display converts to the IP Address $ip_address.\n";
in PHP:
Code:
$ip_address = long2ip($ip_number);
if you are wondering why PHP is shorter, it's because PHP has native call functions. In Perl you build your subroutines, which is why I like perl. It's a true programmer's language. With PHP you just know the native call functions and you're good to go. No programming necessary. lol
I'm going to use APNIC to retrieve the information. In perl of course, and using the WWW::CURL module.
The information retrieved comes in a pipe-character separated format..
apnic|CN|ipv4|202.127.4.0|256|19950610|assigned is an example.
Broken down it's the REGISTRY, COUNTRY_CODE, ADDRESS_TYPE, ADDRESS, NUMBER, DATE, and RANG_TYPE.
Just a quick note about range type: it's either allocated (borrowed), or assigned (owned). ISP's make use of it all the time. It means static or dynamic ip address.
What you want is the second value or the COUNTRY_CODE. If you make a database of your own to convert the codes to actual countries, you're good to go.
Sorry PHP guys. I wouldn't dare touch this is PHP, but that's just me.
Anyway, I am going to build onto my website my own database that you all can use, free of course.