Convert ip address to ip number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert ip address to ip number
# 1  
Old 01-02-2008
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
# 2  
Old 01-02-2008
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";
# 3  
Old 01-02-2008
please skip this comments

never mind, by the time I requested it, you already posted it.
# 4  
Old 01-02-2008
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
# 5  
Old 01-02-2008
Could any one send help me please in this script?
# 6  
Old 01-02-2008
IP to Counrty : PHP & mySQL

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.

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

PHP Code:
<?

    //---------------------------------------------------
    // 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. Smilie
# 7  
Old 01-03-2008
From the command line:
Code:
#!/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:
Code:
#!/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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert floating point to a number

Hello Guys, I have a floating point number 1.14475E+15 I want to convert this number in to full number (Integer or Big integer). I tried couple of functions it did not work. When I use INT=${FLOAT/.*} I am getting value as 1. I don't want a truncated value #!/bin/bash #... (9 Replies)
Discussion started by: skatpally
9 Replies

2. Shell Programming and Scripting

Convert a String to a Number

I read in two numbers from a user but the number is a string. #!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 I know you can use the the "expr" or "bc" command to automatically convert the string to a number then add them together. But I don't want to add... (10 Replies)
Discussion started by: Loc
10 Replies

3. Shell Programming and Scripting

Convert string number to a integer

I have data as below "ROWS merge process complete. thousand rows changed" I need to get a variable assigned the value of 1000. I mean convert the string thousand to 1000. Any help or pointer. Please use CODE tags as required by forum rules! (6 Replies)
Discussion started by: dsravanam
6 Replies

4. Shell Programming and Scripting

Convert number

friends as I can convert this value to number in this example it is well but can vary the value does not help me cut from a nesecito espefifica opsiocn get zero CantCabe = 00023 Cant = 23 CantPntCabe = 0000000000000034 CantPnt = 34 if && ; then echo... (3 Replies)
Discussion started by: tricampeon81
3 Replies

5. Shell Programming and Scripting

How to convert number to english?

Hi gurus, I have a weird requirement. I need to convert the number to english lecture. I have 1.2 ....19 numbers I need to convert to first second third fourth, fifth, sixth... Is there any way convert it using unix command? thanks in advance. (8 Replies)
Discussion started by: ken6503
8 Replies

6. Shell Programming and Scripting

Convert e+ to number

Hi, I am using awk to get particular dates in seconds and the output am getting is like 1.28071e+09. How can I convert it to number format. Can anyone help me out? Thanks in advance..! (7 Replies)
Discussion started by: Kattoor
7 Replies

7. Shell Programming and Scripting

Convert IP address (within a line) to hostname

I have a tricky problem, and I'm quite the scripting newb. I have Cisco ACLs that have IP addresses in them. I'd like to convert the IP's to hostnames for easier analysis. A sample ACL input file would be (I've obfuscated the IPs): access-list acl-secure-out line 1 extended permit icmp any... (3 Replies)
Discussion started by: scolazz
3 Replies

8. Shell Programming and Scripting

bash: convert mac address to 16 character format

Hi there Im not quite sure how i can do this, but i am retrieving the mac address from boxes, which in some instances is arriving in its shortened format (i.e. dropping the leading zeros)... for example 0:3:BA:1:E:84 Im trying to figure out a way of converting the single character... (3 Replies)
Discussion started by: rethink
3 Replies

9. Shell Programming and Scripting

need script to convert number in hexadecimal

hi , i need a script to convert number into hexadecimal base for example: 237=>ED it s very important for me thank you in advance for you help (5 Replies)
Discussion started by: mips
5 Replies

10. Solaris

How can I find the number of connections from a specific IP address historically?

I am using netstat -na command to find out the number of network connections from a specific machine, but this returns information as of now. His there anyway that I can find out this information from yesterday or earlier. Thanks, Tim (3 Replies)
Discussion started by: tkimber
3 Replies
Login or Register to Ask a Question

Featured Tech Videos