Issue
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.
Solution
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
Answered By - ShellFish Answer Checked By - Clifford M. (WPSolving Volunteer)