Ip Location Lookup


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ip Location Lookup
# 1  
Old 11-23-2011
Ip Location Lookup

Hello everybody;

I am working on a script about ip location lookup.

I 've a decimal list
dec_list:
16777216,17367039,AU,AUS,AUSTRALIA
17367040,17432575,MY,MYS,MALAYSIA
17432576,17498111,AU,AUS,AUSTRALIA
17498112,17563647,KR,KOR,REPUBLIC OF KOREA
17563648,17825791,CN,CHN,CHINA
17825792,18087935,KR,KOR,REPUBLIC OF KOREA
18087936,18153471,AU,AUS,AUSTRALIA
18153472,18219007,JP,JPN,JAPAN
18219008,18350079,IN,IND,INDIA
18350080,18874367,CN,CHN,CHINA
18874368,18939903,AU,AUS,AUSTRALIA
bla bla bla...

And I have an ip.log
2.16.170.4
8.16.170.4
7.16.170.4
6.16.170.4
5.16.170.4
4.16.170.4
2.13.173.6
2.16.170.5
78.161.18.60
14.16.170.4
13.16.170.4
12.16.170.4
11.13.173.6
bla bla bla...


So my script:
Code:
#!/bin/bash

IP_FILE=ip.log
DECIMAL_FILE=dec_list1

declare -a ip_dec_list ip_list

ip_list=(  $(sed '/^$/d' "${IP_FILE}") ) 

for ((i=0; i<${#ip_list[@]}; i++))
{
  IP=${ip_list[$i]//./ }
  set -- $IP
  ip_dec_list+=( $(( 256*256*256 * $1 + 256*256 * $2 + 256 * $3 + $4 )) )
}
printf "%s\n" "${ip_dec_list[@]}">arr
arr=arr

IFS=','
while read -r bilgi
do
  set -- $bilgi
for ((i=0; i<=${#ip_dec_list[@]}; i++))
  {
    if  [[ ${ip_dec_list[$i]} -gt $1 ]]  &&  [[ ${ip_dec_list[$i]} -lt $2 ]]
    then
          printf '\n%s\n%-12s%s\n%-12s%s\n%-12s%s\n%s\n ' \
          '*******************************************' \
            "IP  " "= ${ip_list[$i]}" \
            "IP Decimal"  "= ${ip_dec_list[$i]} " \
            "Ulke  " "= ${3//_/ } - $1 ($2) " \
            '*******************************************'
 
    fi
 } 
done < "${DECIMAL_FILE}"


Yes all of this. The problem is my script does not work for line-up in the ip.log and does not find for all ips in the ip.log. I can't understand why it is. But I need a help.. I searched for this a lot of times but I haven't found a info about this which is for me..
I Wait for your help..
# 2  
Old 11-23-2011
It would be very helpful if you display the desired output.
# 3  
Old 11-23-2011
sorry,
that' the output. If u re check it you will see the line up is not same with ip log file.


Code:
*******************************************
IP          = 2.13.173.6
IP Decimal  = 34450694 
Ulke        = FR - 33554432 (34603007) 
*******************************************
 
*******************************************
IP          = 2.16.170.5
IP Decimal  = 34646533 
Ulke        = DE - 34646528 (34647551) 
*******************************************
 
*******************************************
IP          = 4.16.170.4
IP Decimal  = 68200964 
Ulke        = US - 50331648 (68257567) 
*******************************************
 
*******************************************
IP          = 4.20.73.17
IP Decimal  = 68438289 
Ulke        = CA - 68438288 (68438303) 
*******************************************
 
*******************************************
IP          = 5.16.170.4
IP Decimal  = 84978180 
Ulke        = EU - 83886080 (88080383) 
*******************************************
 
*******************************************
IP          = 7.16.170.4
IP Decimal  = 118532612 
Ulke        = US - 100663296 (121195295) 
*******************************************
 
*******************************************
IP          = 6.16.170.4
IP Decimal  = 101755396 
Ulke        = US - 100663296 (121195295) 
*******************************************
 
*******************************************
IP          = 8.16.170.4
IP Decimal  = 135309828 
Ulke        = US - 135200768 (135603199) 
*******************************************
 
*******************************************
IP          = 11.13.173.6
IP Decimal  = 185445638 
Ulke        = US - 184549376 (201405503) 
*******************************************
 
*******************************************
IP          = 12.16.170.4
IP Decimal  = 202418692 
Ulke        = US - 202385920 (202517983) 
*******************************************
 
*******************************************
IP          = 13.16.170.4
IP Decimal  = 219195908 
Ulke        = US - 217046784 (234881023) 
*******************************************
 
*******************************************
IP          = 14.16.170.4
IP Decimal  = 235973124 
Ulke        = CN - 235929600 (236978175) 
*******************************************
 
*******************************************
IP          = 38.8.6.35
IP Decimal  = 638060067 
Ulke        = US - 637534208 (644067391) 
*******************************************
 
*******************************************
IP          = 78.161.18.60
IP Decimal  = 1319178812 
Ulke        = TR - 1319108608 (1321205759) 
*******************************************

---------- Post updated at 12:16 PM ---------- Previous update was at 12:08 PM ----------

this is the another output for you. I append a command for show i variable. Sıra is meaning line up. I think this is easier than the previous one.

Code:
*******************************************
Sıra =     6
IP          = 2.13.173.6
IP Decimal  = 34450694 
Ulke  
 
= FR - 33554432 (34603007) 
*******************************************
            
            

 
*******************************************
Sıra =     7
IP          = 2.16.170.5
IP Decimal  = 34646533 
Ulke  
 
= DE - 34646528 (34647551) 
*******************************************
            
            

 
*******************************************
Sıra =     5
IP          = 4.16.170.4
IP Decimal  = 68200964 
Ulke  
 
= US - 50331648 (68257567) 
*******************************************
            
            

 
*******************************************
Sıra =     0
IP          = 4.20.73.17
IP Decimal  = 68438289 
Ulke  
 
= CA - 68438288 (68438303) 
*******************************************
            
            

 
*******************************************
Sıra =     4
IP          = 5.16.170.4
IP Decimal  = 84978180 
Ulke  
 
= EU - 83886080 (88080383) 
*******************************************
            
            

 
*******************************************
Sıra =     2
IP          = 7.16.170.4
IP Decimal  = 118532612 
Ulke  
 
= US - 100663296 (121195295) 
*******************************************
            
            

 
*******************************************
Sıra =     3
IP          = 6.16.170.4
IP Decimal  = 101755396 
Ulke  
 
= US - 100663296 (121195295) 
*******************************************
            
            

 
*******************************************
Sıra =     1
IP          = 8.16.170.4
IP Decimal  = 135309828 
Ulke  
 
= US - 135200768 (135603199) 
*******************************************
            
            

 
*******************************************
Sıra =     12
IP          = 11.13.173.6
IP Decimal  = 185445638 
Ulke  
 
= US - 184549376 (201405503) 
*******************************************
            
            

 
*******************************************
Sıra =     11
IP          = 12.16.170.4
IP Decimal  = 202418692 
Ulke  
 
= US - 202385920 (202517983) 
*******************************************
            
            

 
*******************************************
Sıra =     10
IP          = 13.16.170.4
IP Decimal  = 219195908 
Ulke  
 
= US - 217046784 (234881023) 
*******************************************
            
            

 
*******************************************
Sıra =     9
IP          = 14.16.170.4
IP Decimal  = 235973124 
Ulke  
 
= CN - 235929600 (236978175) 
*******************************************
            
            

 
*******************************************
Sıra =     14
IP          = 38.8.6.35
IP Decimal  = 638060067 
Ulke  
 
= US - 637534208 (644067391) 
*******************************************
            
            

 
*******************************************
Sıra =     8
IP          = 78.161.18.60
IP Decimal  = 1319178812 
Ulke  
 
= TR - 1319108608 (1321205759) 
*******************************************

---------- Post updated at 12:59 PM ---------- Previous update was at 12:16 PM ----------

OK! : )

I solved it...,

The problem resulted from For Loop. I positioned the wrong line for loop. It must be top of the while. So it is working according to lines in ip list.
In the previous one was working according to decimal list.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with copying the list of files from one location to other location

A) I would like to achive following actions using shell script. can someone help me with writing the shell script 1) Go to some dir ( say /xyz/logs ) and then perform find operation in this dir and list of subdir using find . -name "*" -print | xargs grep -li 1367A49001CP0162 >... (1 Reply)
Discussion started by: GG2
1 Replies

2. Shell Programming and Scripting

How to find a existing file location and directory location in Solaris box?

Hi This is my third past and very impressed with previous post replies Hoping the same for below query How to find a existing file location and directory location in solaris box (1 Reply)
Discussion started by: buzzme
1 Replies

3. Shell Programming and Scripting

How to copy a file from one location to another location?

I have file file1.txt in location 'loc1'. Now i want a copy of this file in location 'loc2' with a new file called test.txt. Please help me how to do this in shell script. (1 Reply)
Discussion started by: vel4ever
1 Replies

4. Shell Programming and Scripting

File created in a different location instead of desired location on using crontab

Hi, I am logging to a linux server through a user "user1" in /home directory. There is a script in a directory in 'root' for which all permissions are available including the directory. This script when executed creates a file in the directory. When the script is added to crontab, on... (1 Reply)
Discussion started by: archana.n
1 Replies

5. Shell Programming and Scripting

Help on Moving files from one location to another location

Hi, I am new to unix and shell scripting. Please help me in resolving the below issue. In my shell script I have a variable which stores the different files with the path. Now I need to move all the files one by one to another location. ---- 1.... (4 Replies)
Discussion started by: kpagadala
4 Replies

6. Shell Programming and Scripting

Shell Script for Copy files from one location to another location

Create a script that copies files from one specified directory to another specified directory, in the order they were created in the original directory between specified times. Copy the files at a specified interval. (2 Replies)
Discussion started by: allways4u21
2 Replies

7. Shell Programming and Scripting

Put one string from one location to another location in a file

Hi Everyone, I have 1.txt here a b c' funny"yes"; d e The finally output is: here a b c d e' funny"yes"; (1 Reply)
Discussion started by: jimmy_y
1 Replies

8. Shell Programming and Scripting

Transfer files from one location to another location

Hi, i have to transfer of files of User1 located in Location1 to user2 located in Location2 using shell script. Please suggest me. (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

9. UNIX for Advanced & Expert Users

copy files from one location to similar location

I need help in forming a script to copy files from one location which has a sub directory structure to another location with similar sub directory structure, say location 1, /home/rick/tmp_files/1-12/00-25/ here 1-12 are the number of sub directories under tmp_files and 00-25 are sub... (1 Reply)
Discussion started by: pharos467
1 Replies

10. UNIX for Advanced & Expert Users

Clueless about how to lookup and reverse lookup IP addresses under a file!!.pls help

Write a quick shell snippet to find all of the IPV4 IP addresses in any and all of the files under /var/lib/output/*, ignoring whatever else may be in those files. Perform a reverse lookup on each, and format the output neatly, like "IP=192.168.0.1, ... (0 Replies)
Discussion started by: choco4202002
0 Replies
Login or Register to Ask a Question