4

I've a list of subnet range in a file:

2.32.0.0-2.47.255.255-255.240.0.0
2.112.0.0-2.119.255.255-255.248.0.0
2.156.0.0-2.159.255.255-255.252.0.0
2.192.0.0-2.199.255.255-255.248.0.0
...

(The file format is: {startip}-{endip}-{netmask})

I need check if an IP is included in one of the subnet in the file.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Stefano Guarella
  • 59
  • 1
  • 1
  • 6

6 Answers6

3

You may use awk for that:

echo '127.0.0.0-127.255.255.255-255.0.0.0' | awk -F- '
    BEGIN { ip[1] = 127; ip[2] = 0; ip[3] = 0; ip[4] = 1; } 
    { split($1, startIp, "."); split($2, endIp, ".");
        for(i = 1; i <= 4; i++) {
            if(ip[i] < int(startIp[i]) || ip[i] > int(endIp[i]))
                break;
        }

        if(i == 5)
            print "matching line: ", $0; }'

IP for searching is initially set as array in BEGIN-clause as array. Each line is compared in for-cycle and if each octet laying between startIp and endIp, "matching line" is printed.


Some Python 3 gibberish relying on ipaddress module from 3.3 (available for 2.6/2.7:

python3 -c 'from ipaddress import ip_address as IP; list(
    map(print, ((startip, endip) for startip, endip, _ in 
            (ip.split("-") for ip in open("tmp/iplist.txt")) 
            if IP(startip) < IP("127.0.0.1") < IP(endip))))'

Which is actually one-liner version for following script:

import sys
from ipaddress import ip_address as IP

ip = IP(sys.argv[1])

with open(sys.argv[2]) as f:
    for line in f:
        startIp, endIp, _ = line.split('-')
        if IP(startIp) < ip < IP(endIp):
            print(line)

Which can be used like that:

$ python3 ipcheck.py 127.0.0.1 iplist.txt
myaut
  • 11,174
  • 2
  • 30
  • 62
1

Try this:

BEGIN {
    FS="."
    ex = "false"
    split(address, ip, ".")
}
{
    split($0, range, "[-.]")
    for (i=1; i<5; i++) {
        if (ip[i] < range[i] || ip[i] > range[i+4])
            break;
        else if ((ip[i] > range[i] && ip[i] < range[i+4]) || i == 4)
            ex = "true"
    }
}
END {
    print ex
}

Invoke this awk script (checkIP.awk) like this:

$ awk -v address="2.156.0.5" -f checkIP.awk /path/to/ip/ranges/file
true
$ awk -v address="0.0.0.0" -f checkIP.awk /path/to/ip/ranges/file
false
ShellFish
  • 4,351
  • 1
  • 20
  • 33
  • Thanks a lot ShellFish, but unfortunately it doesn't work with some IP, I give you an example: 31.207.96.0-31.207.127.255-255.255.224.0 $ awk -v address="31.207.96.5" -f checkIP.awk itaIP.list false $ awk -v address="31.207.97.5" -f checkIP.awk itaIP.list false – Stefano Guarella May 26 '15 at 15:13
1

You can use this awk script:

awk -F- -v arg='2.158.1.2' 'function ipval(arg) {
   split(arg, arr, ".");
   s=0;
   for (i=1; i<=length(arr); i++)
      s += arr[i] * (10**(6-i));
   return s   
}
ipval(arg) >= ipval($1) && ipval(arg) <= ipval($2)' file
2.156.0.0-2.159.255.255-255.252.0.0

ipval converts given ip address to a numeric value so that it can be compared easily using arithmetical operator.

anubhava
  • 761,203
  • 64
  • 569
  • 643
1

Check if IP is valid and if it's on a local connection:

if ! ip route get ${ip} | grep -v via > /dev/null;then echo "bad IP";fi
GET
  • 69
  • 5
0

I find this variant of the above script more useful:

function ipval()
{
  RES=`awk -F- -v arg="$1" '{
    split(arg, arr, ".");
    s=0;
    for (i=1; i<=length(arr); i++) { s += arr[i] * (256**(4-i)); }
    print s
  }' <<< '' `
  return $RES
}

read -p "IP:" IP
ipval "$IP"
echo "RES=$RES"
0

grepcidr command line tool can help in such sort of tasks.

COMMAND USAGE
-------------
Usage:
        grepcidr [-V] [-cCDvhais] PATTERN [FILE ...]
        grepcidr [-V]  [-V] [-cCDvhais] [-e PATTERN | -f FILE] [FILE ...]

Each pattern, whether on the command line or inside a file, may be:
CIDR format a.b.c.d/xx or aa:bb::cc::dd/xx
IP range    a.b.c.d-e.f.g.h
Single IP   a.b.c.d or aa:bb:cc::dd
Oleg Neumyvakin
  • 9,706
  • 3
  • 58
  • 62