Checking range of ips


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking range of ips
# 8  
Old 11-07-2013
Yes this is a little inefficient as it calculates the value of the input IP twice for each line.

this should work a little quicker:

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]
}
BEGIN {v=ip2dec(I)}
v>=ip2dec($1) && v<=ip2dec($2){print $6}' country.txt

---------- Post updated at 06:39 AM ---------- Previous update was at 06:32 AM ----------

Can also get rid of the gsub() call :

Code:
awk -F '[",]+' -v I="218.30.103.106" '
function ip2dec(ip, slice) {
        split(ip, slice, ".")
        return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4]
}
BEGIN {v=ip2dec(I)}
v>=ip2dec($2) && v<=ip2dec($3){print $7}' country.txt

This User Gave Thanks to Chubler_XL For This Post:
# 9  
Old 11-07-2013
Quote:
Originally Posted by Chubler_XL
Yes this is a little inefficient as it calculates the value of the input IP twice for each line.

this should work a little quicker:

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]
}
BEGIN {v=ip2dec(I)}
v>=ip2dec($1) && v<=ip2dec($2){print $6}' country.txt

---------- Post updated at 06:39 AM ---------- Previous update was at 06:32 AM ----------

Can also get rid of the gsub() call :

Code:
awk -F '[",]+' -v I="218.30.103.106" '
function ip2dec(ip, slice) {
        split(ip, slice, ".")
        return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4]
}
BEGIN {v=ip2dec(I)}
v>=ip2dec($2) && v<=ip2dec($3){print $7}' country.txt

the first example, with the gsub included appears to be the fastest.

again thank you Smilie
# 10  
Old 11-07-2013
If you have a file full of IPs to lookup it can be made more efficient again.
# 11  
Old 11-07-2013
Quote:
Originally Posted by Chubler_XL
If you have a file full of IPs to lookup it can be made more efficient again.
really? how so? i'd really like to know. i just tried it on a list of IPs and it took a while.
# 12  
Old 11-07-2013
something like this:

Code:
awk -F '[",]+' '
function ip2dec(ip, slice) {
    split(ip, slice, ".")
    return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4]
}
FNR==NR{n[ip2dec($1)]=$1;next}
{
   f=ip2dec($2);
   t=ip2dec($3);
   for(v in n) if(v>=f&&v<=t) {
       print n[v] "," $7;
       delete n[v];
   }
}
END {for(v in n) print n[v] ",UNKNOWN" } ' ips.txt country.txt

This User Gave Thanks to Chubler_XL For This Post:
# 13  
Old 11-07-2013
Quote:
Originally Posted by Chubler_XL
something like this:

Code:
awk -F '[",]+' '
function ip2dec(ip, slice) {
    split(ip, slice, ".")
    return (slice[1] * 2^24) + (slice[2] * 2^16) + (slice[3] * 2^8) + slice[4]
}
FNR==NR{n[ip2dec($1)]=$1;next}
{
   f=ip2dec($2);
   t=ip2dec($3);
   for(v in n) if(v>=f&&v<=t) {
       print n[v] "," $7;
       delete n[v];
   }
}
END {for(v in n) print n[v] ",UNKNOWN" } ' ips.txt country.txt


now that's just marvelous!!!!!!!!!!!!
# 14  
Old 11-08-2013
If your file is reliably set up in a way that $4 and $5 are the decimal notations of $2 and $3, then using those instead of f and t (eliminating 2 ip2dec calls per line!) will again speed things up (a little).
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