Checking range of ips


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking range of ips
# 1  
Old 11-07-2013
Checking range of ips

Given a range of IPs similar to this:

Code:
"212.63.183.19","212.63.183.19","3560945427","3560945427","CN","China"
"217.7.143.0","217.7.143.0","3641151232","3641151232","CN","China"
"218.0.0.0","218.31.255.255","3657433088","3659530239","CN","China"
"218.56.0.0","218.99.255.255","3661103104","3663986687","CN","China"
"218.100.88.0","218.100.255.255","3664009216","3664052223","CN","China"
"218.104.0.0","218.109.255.255","3664248832","3664642047","CN","China"
"218.185.192.0","218.185.223.255","3669606400","3669614591","CN","China"
"223.165.4.0","223.165.7.255","3752133632","3752134655","ID","Indonesia"
"223.165.8.0","223.165.15.255","3752134656","3752136703","TW","Taiwan"
"223.165.16.0","223.165.19.255","3752136704","3752137727","NZ","New Zealand"
"223.165.20.0","223.165.23.255","3752137728","3752138751","JP","Japan"
"223.165.24.0","223.165.31.255","3752138752","3752140799","IN","India"

How do you find out which country the below IP belongs to?

Code:
218.30.103.106

some awk or grep command can probably be used here. but i'm hoping someone with some knowledge on IPs can shed light on the proper way to do this.

i'm going to have a list of specific IP addresses and i need to figure out under which country they correspond.
# 2  
Old 11-07-2013
This User Gave Thanks to blackrageous For This Post:
# 3  
Old 11-07-2013
Convert the quad-dotted ip address to decimal and check if it falls within the inclusive range specified by the third and fourth columns in your file (which are the decimal equivalents of columns 1 and 2).
Code:
A.B.C.D = A*2^24 + B*2^16 + C*2^8 + D

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 11-07-2013
Here's the easy route:

Code:
curl http://api.hostip.info/get_html.php?ip=218.30.103.106
Country: CHINA (CN)
City: (Unknown city)
IP: 218.30.103.106

This User Gave Thanks to in2nix4life For This Post:
# 5  
Old 11-07-2013
Quote:
Originally Posted by in2nix4life
Here's the easy route:

Code:
curl http://api.hostip.info/get_html.php?ip=218.30.103.106
Country: CHINA (CN)
City: (Unknown city)
IP: 218.30.103.106

thank you so much. this was actually my original option. but, i had to take into account the possibility of the site (api.hostip.info) not being up 100% of the time (not that it has ever been an issue in the past). but i have to account for that. i like this option. its pretty straightforward.

was just wondering if there's another way to get this information from a list that contains the range of IPs and their countries, which is what i pasted in my first post. this list will be on my local server which is guaranteed to be available 100% of the time.
# 6  
Old 11-07-2013
Here is a quick and dirty awk script
Code:
awk -F, -v I="218.30.103.106" '
function ip2dec(ip, slice) {
    gsub(/"/,"",ip)
    split(ip, slice, ".")
    return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4]
}
ip2dec(I)>=ip2dec($1) && ip2dec(I)<=ip2dec($2){print $6}' country.txt

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 11-07-2013
Quote:
Originally Posted by Chubler_XL
Here is a quick and dirty awk script
Code:
awk -F, -v I="218.30.103.106" '
function ip2dec(ip, slice) {
    gsub(/"/,"",ip)
    split(ip, slice, ".")
    return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4]
}
ip2dec(I)>=ip2dec($1) && ip2dec(I)<=ip2dec($2){print $6}' country.txt

thank you so very much. i would not have been able to figure this out on my own. and i really really really appreciate you taking the time.

you wouldn't happen to know of a way to make the script run faster, would you? the Country.txt file is about 6MB in size. and to check one IP took about 2-3 seconds. i intend to run your script on a hundreds of IPs. so it'll be great if there's a way to make it fast.

again. thank you so much for this!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

IPS postinstall script

Hi guys, I'm creating custom package for deploying a bunch of public keys for some root servers we have. I have created script and it works but my problem is how to call this script when I want deploy package with puppet on server? Oracle documentation provided only example for first boot script... (0 Replies)
Discussion started by: solaris_user
0 Replies

2. Solaris

Creating IPS packaging

I have installed WL on Solaris 11 and have to create IPS packaging for following directories so that /local/apps/wl/wls12.x /local/apps/wl/oraInventory /local/apps/wl/jdk/jdk1.x Can someone provide the steps for creating IPS or point to the website where I can get help. Thanks,... (2 Replies)
Discussion started by: sam101
2 Replies

3. Solaris

Updating my Solaris 11 IPS

Hi experts, I have an X86 as my IPS server running ( uname -a SunOS 5.11 11.1 i86pc i386 i86pc). the IPS has the following package version: prdb01b:~# pkg list -fa entire NAME (PUBLISHER) VERSION IFO entire ... (0 Replies)
Discussion started by: afadaghi
0 Replies

4. SCO

Stop boot system at "Checking protected password and checking subsystem databases"

Hi, (i'm sorry for my english) I'm a problem on boot sco unix 5.0.5 open server. this stop at "Checking protected password and checking subsystem databases" (See this image ) I'm try this: 1) http://www.digipedia.pl/usenet/thread/50/37093/#post37094 2) SCO: SCO Unix - Server hangs... (9 Replies)
Discussion started by: buji
9 Replies

5. Fedora

Using iptables to allow only certain IPs for a Port

Hi.. Anyone can help me..I have setup my linux fedora server and i want to restrict access to my server.Basically i control using iptables.I'm not sure how to write an iptables rules to control drop all connection to port 8080 and allow only certain ip can access the instance on port 8080 example... (0 Replies)
Discussion started by: netxus
0 Replies

6. Red Hat

Which IPs from the range are assigned to DHCP enabled clients

Hi All dhcpd.conf has a range of IPs sa for example( 192.168.1.201 192.168.1.220) So this is the range of IP addresses the server will issue to DHCP enabled PC clients booting up on the network How do i know which IPs are being used or which IPs from the range are assigned to dhcp enabled... (11 Replies)
Discussion started by: tannu
11 Replies

7. Shell Programming and Scripting

Shell script to ping a range of IPs

Hi Can someone give me a shell script that can ping a range of IPs and return IPs which are not pingable. Range for example say 192.168.0.1 to 192.168.0.50 and whichever are not pingable then return the IP. Thanks for your help (3 Replies)
Discussion started by: tannu
3 Replies

8. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies

9. Shell Programming and Scripting

checking a variable is within range

how can i check that a variable is between 0-100, like if i ask a user to input a number between 1-100 and i want to excute commands WHILE that number is between that range or else i will keep asking the user to make another input here's what i got #!/bin/bash echo "Guess my secret number... (2 Replies)
Discussion started by: Poison Ivy
2 Replies

10. Solaris

Changing IPs on E450

Hi, I'm working with an E450 server and it has one network card configured with 2 local and internet IP addresses. Presently, the local IP is set as the primary address while teh Internet IP is set as the secondary address. I wish to change this and make the local IP secondary and the... (2 Replies)
Discussion started by: Ronny
2 Replies
Login or Register to Ask a Question