I want to make a script to read a list of ip addresses from a file then convert those ip addresses to ip number.
ip number is made by removing the dots then transfer to a number , so the ip number transfered to binary then to decimal which will represents the ip number 0 : 4294967295
Also I want to make the reverse operation to transfer from ip number to real ip with dots.
This is all to make a script to search for each ip in which country
suppose the ip 10.11.15.200, this ip can be transfered to binary as follow to be 00001010.00001011.00001111.11001000 then to erase the dots to be 00001010000010110000111111001000 then to transfer to decimal to be 168497096
then if I made the reverse process it will give the same ip, by transfer the decimal to binary then put a dot after each 8 digits then convert each 8 binary digits to decimal
The file shape as follow:
1.1.1.1
100.50.64.55
99.81.56.88
.........
Also I want to make another script to make the reverse process, example: entering the ip number list and get the ip addresses
The final script I want to make is to determine what is the location of each ip, so after this script I will try to get all database of ripe.net (afriNIC-ARIN) then have a database of each ip number in which country
i was looking around to convert 127.0.0.1 to a number, something that all IP to Counry Databse left out, and stumbled upon your post.
i have some information that may be of help to you.
i have done a module do to what you are trying to do however it is for a particular CMS and in PHP language. You may wish to decode it into whatever language to you. In doing so, i went through the net looking for resources and here is one i found to be reliable and free. IP-to-Country.com | 'Cause every I.P has a Home...
Here are the relevant info...
There is a free IP to Country Database HERE. This is the dB i am using and is adequate as i want something simple and non commercial.
2.Converting IP address is done as shown HERE. Your method maybe a little long, so you may wish to check out this recommended one. There are also several other easier and more efficient ways posted by others on the thread i gave above. Take your pick.
World Flags.
I found the flags by w ww.hahn-hotel.com to be the best, but you have many to choose from.
===============
The PHP example below is taken from the same website, where all the above info comes from. Note however, that IP to Number conversion is done using the mySQL "inet_aton" function which does the conversion, so if you use mySQL, you do not need to convert the IP, which is very convenient.
<?
//---------------------------------------------------
// Sample code to display Visitor Country information
// PHP 4
//---------------------------------------------------
// Establishing a database connection
$dbh=mysql_connect("localhost:3306","$MYSQL_USERNAME","$MYSQL_PASSWORD");
mysql_select_db("$MYSQL_DBNAME");
// Query for getting visitor countrycode
$country_query = "SELECT country_code2,country_name FROM iptoc ".
"WHERE IP_FROM<=inet_aton('$REMOTE_ADDR') ".
"AND IP_TO>=inet_aton('$REMOTE_ADDR') ";
// Executing above query
$country_exec = mysql_query($country_query);
// Fetching the record set into an array
$ccode_array=mysql_fetch_array($country_exec);
// getting the country code from the array
$country_code=$ccode_array['country_code2'];
// getting the country name from the array
$country_name=$ccode_array['country_name'];
// Display the Visitor coountry information
echo "$country_code - $country_name";
// Closing the database connection
mysql_close($dbh);
?>
This PHP is of course using the free database provided on the website, but note that this example program is a little off because the database have been updated and the field names are no longer in existence. However, the example serves as a good example.
#!/usr/bin/perl
#
$ip_address = $ARGV[0];
@octets = split(/\./, $ip_address);
$DEC = ($octets[0]*1<<24)+($octets[1]*1<<16)+($octets[2]*1<<8)+($octets[3]);
print "The IP Address $ip_address converts to decimal $DEC\n"
output:
~/code/iptodec $ ./iptodec.pl 1.1.1.1
The IP Address 1.1.1.1 converts to decimal 16843009
From a file:
#!/usr/bin/perl
#
open(FILE, "IPs");
while (<FILE>) {
chomp($ip_address = $_);
@octets = split(/\./, $ip_address);
$DEC = ($octets[0]*1<<24)+($octets[1]*1<<16)+($octets[2]*1<<8)+($octets[3]);
print "The IP Address $ip_address converts to decimal $DEC\n"
}
close(FILE);
output:
n@p ~/code/iptodec $ cat IPs
1.1.1.1
100.50.64.55
99.81.56.88
n@p ~/code/iptodec $ ./iptodec2.pl
The IP Address 1.1.1.1 converts to decimal 16843009
The IP Address 100.50.64.55 converts to decimal 1681014839
The IP Address 99.81.56.88 converts to decimal 1666267224
Let me explain the content of this file, First two letters represents the location of ip address subnet, second column represents the start of ip subnet, the third column represents how many ips in this subnet.
I thought about after transfer the second column to ip number using the previous script, then I have the start of subnet, then by addition of the number of ips in the third column..... Now I have all the range of ip number in this area which belongs to this country.
The final step is to compare the ip numbers I've had before with this file to know the location of each ip.
Pleaze, Pleaze help me in this script, if you have any inquiries send me
You can read in the file in the hash, with the IP address as the key and the location as the value. Then, a $hash{$ipaddr} will give you the location, if exists.
#!/bin/sh
while read ip
do
s=""
num=`echo $ip | tr "." " "` #convert all dots to space
for n in $num
do
s=$s`echo "obase=2;$n" | bc`
done
echo "ibase=2;obase=A;$s" | bc # convert binary to decimal
done < "file"
I leave you todo the reverse if you want. See here for the reference
The file content I have is like below, what I understand is that the range 33996344 to 33996351 of ip numbers is located in USA and so the rest of file,
How to compare a list of ip numbers in a file with that file and get the final location of each ip?
33996344 33996351 UNITED KINGDOM
50331648 69956103 UNITED STATES
69956104 69956111 BERMUDA
69956112 83886079 UNITED STATES
94585424 94585439 SWEDEN
100663296 121195295 UNITED STATES
121195296 121195327 ITALY
121195328 152305663 UNITED STATES
152305664 152338431 UNITED KINGDOM
152338432 167772159 UNITED STATES
184549376 201620303 UNITED STATES
201620304 201620311 CANADA
201620312 201674095 UNITED STATES
201674096 201674111 CANADA
201674112 201859071 UNITED STATES
I am tottaly agree that this file is helpful, but still can't make the script which have this file and the file of my ip numbers and make comparison to produce each ip located in which country.