Convert ip address to ip number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert ip address to ip number
# 8  
Old 01-02-2008
Quote:
Originally Posted by moutaz1983
I want to make a script to read a list of ip addresses from a file then convert those ip addresses to ip number.
What is your intent with regards to IPv6 addresses?
# 9  
Old 01-03-2008
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
# 10  
Old 01-03-2008
Try to use pack() and unpack().

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

which gives "168497096".

To reverse, it's just a reverse:

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

For this sort of things, doing the bit-level manipulations manually is IMO quite noisy.
# 11  
Old 01-03-2008
Quote:
Originally Posted by moutaz1983
The final step is to compare the ip numbers I've had before with this file to know the location of each ip.
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.
# 12  
Old 01-03-2008
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
# 13  
Old 01-03-2008
Code:
#!/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
# 14  
Old 01-03-2008
Lightbulb

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