I have file with allocated IP address ranges. It looks something like this:
...
arin|US|ipv4|8.0.0.0|16777216|19921201|allocated
arin|US|ipv4|9.0.0.0|16777216|19881216|assigned
apnic|ID|ipv4|122.200.144.0|2048|20061023|allocated
apnic|TW|ipv4|122.200.152.0|2048|20080424|allocated
apnic|AU|ipv4|122.200.160.0|4096|20061020|allocated
apnic|AU|ipv4|122.200.176.0|4096|20110121|allocated
apnic|JP|ipv4|122.200.192.0|8192|20061023|allocated
...
My question is if it is possible and how to get specific row from this file using an IP address as search parameter in Linux by using tools like grep, awk or some other tools.
e.g. if the searched IP is 8.8.8.8 the result should be:
arin|US|ipv4|8.0.0.0|16777216|19921201|allocated
EDIT// FUll ipv4 list can be found here http://skechboy.com/ips/ipv4_table
Here is the awk implementation of @ugoren method:
awk
can be used, this way:1. Run with the
-F'|'
parameter, to separate by the vertical bar. Now$4
is the first address,$5
is the network size.2. Use
split($4,addr,'.')
To get an arraya
with the 4 address parts3. Do some math to get the numeric address:
range_start=a[0]*16777216+a[1]*65536+a[2]*256+a[3]
.4. Do the same math on the address you're looking for.
5. Now just check if the address is in the range, between
range_start
andrange_start+$5
if 4th and 5th columns are network prefix and subnet mask then to grep input for an ip:
Example
Output