Convert ip address to ip number

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

For example ip address 1.1.1.2 will be represented as 00000001000000010000000100000010 and after transfer to decimal will be 16843010

I made the following script it may help, but it doesn't work fine

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 "The IP Address $ip_address converts to the IP Number $ip_number \n";

never mind, by the time I requested it, you already posted it.

Thanks for your care.

Let me explain first on one ip.

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

Thanks in advance

Could any one send help me please in this script?

Hello,

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...

  1. 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.

  1. World Flags.
    I found the flags by w ww.hahn-hotel.com to be the best, but you have many to choose from.

The FLAGS are all listed and downloadable HERE

===============
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.

Hope this helps. :eek:

From the command line:

#!/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

What is your intent with regards to IPv6 addresses?

Hello All thanks for update and the script.

Let continue the challenge, Now we converted the ip addresses to ip number.

If I have a file contain the data as follow:

JP|58.0.0.0|131072
IN|58.2.0.0|65536
JP|58.3.0.0|32768
JP|58.3.128.0|32768
JP|58.4.0.0|131072
AU|58.6.0.0|32768
AU|58.6.128.0|32768
AU|58.7.0.0|65536
TH|58.8.0.0|131072
TH|58.10.0.0|131072
JP|58.12.0.0|131072
CN|58.14.0.0|131072
CN|58.16.0.0|65536

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

Try to use pack() and unpack().

$ipaddr = '10.11.15.200';
print unpack('N', pack('C4', split(/\./, $ipaddr)));

which gives "168497096".

To reverse, it's just a reverse:

$ipnum = '168497096';
print join('.', unpack('C4', pack('N', $ipnum)));

For this sort of things, doing the bit-level manipulations manually is IMO quite noisy.

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.

Could you explain in more details what is meant by hashing.

To make the issue more clear, I have a list of ip addresses in a file, I want to know the location of each ip address.

Could you send me the script complete in Perl. or equivalent method

#!/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

For more and more clarification

Could you help me with the other request?

File 1: Location|subnet

JP|58.0.0.0
IN|58.2.0.0
JP|58.3.0.0
JP|58.3.128.0
JP|58.4.0.0
AU|58.6.0.0
AU|58.6.128.0
AU|58.7.0.0
TH|58.8.0.0
TH|58.10.0.0
JP|58.12.0.0
CN|58.14.0.0
CN|58.16.0.0

File 2: subnet to ip number

973078528
973209600
973275136
973307904
973340672
973471744
973504512
973537280
973602816
973733888
973864960
973996032
974127104
974192640

File 3: ip addresses to ip number

1139043322
1224208041
1094078762
1506936493
3651953455
1280633240
1161801768
1510897669

I want to compare the ip numbers in File 3 with File 2, say that 1139043322 in the some range in File 2, so this ip will be in country XX from File 1

This is the full IP number by country.

http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip

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

Digging in my old MkLinux PPC, found it. Interesting enough, the file is still alive.

here it is, I hope this helps

www.cs.princeton.edu/introcs/datafiles/GeoIPCountryWhois.csv

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.