1

I have a list of ip adrresses. I want to know if any of them is a reserved address (ref: http://en.wikipedia.org/wiki/Reserved_IP_addresses ). For my works I'm using awk so I'm wondering if there are some pre-made shell scripts or similar that could help me in identifyng such addresses.

papafe
  • 2,959
  • 4
  • 41
  • 72

2 Answers2

0

If you feed your list of good old IPv4 addresses line by line as input to the script

while IFS=. read A B C D
do  ADDR=`printf %03d.%03d.%03d.%03d $A $B $C $D`
    [[ 000.255.255.255 < $ADDR && $ADDR < 010.000.000.000 ]] && continue
    [[ 010.255.255.255 < $ADDR && $ADDR < 100.064.000.000 ]] && continue
    [[ 100.127.255.255 < $ADDR && $ADDR < 127.000.000.000 ]] && continue
    [[ 127.255.255.255 < $ADDR && $ADDR < 169.254.000.000 ]] && continue
    [[ 169.254.255.255 < $ADDR && $ADDR < 172.016.000.000 ]] && continue
    [[ 172.031.255.255 < $ADDR && $ADDR < 192.000.000.000 ]] && continue
    [[ 192.000.000.007 < $ADDR && $ADDR < 192.000.002.000 ]] && continue
    [[ 192.000.002.255 < $ADDR && $ADDR < 192.088.099.000 ]] && continue
    [[ 192.088.099.255 < $ADDR && $ADDR < 192.168.000.000 ]] && continue
    [[ 192.168.255.255 < $ADDR && $ADDR < 198.018.000.000 ]] && continue
    [[ 198.019.255.255 < $ADDR && $ADDR < 198.051.100.000 ]] && continue
    [[ 198.051.100.255 < $ADDR && $ADDR < 203.000.113.000 ]] && continue
    [[ 203.000.113.255 < $ADDR && $ADDR < 224.000.000.000 ]] && continue
    echo $A.$B.$C.$D is reserved
done

, it will output any reserved addresses.

Armali
  • 18,255
  • 14
  • 57
  • 171
0

Do you know about unix grep ?

You can have grep search for a list of target strings stored in a file, in your other files.

 fgrep -f reservedList.txt yourListOfIpsSavedToAfile

will print any lines in yourList* that matches any word found in reservedList.

If you are doing this inside a shell script, then you can make your actions conditional

 if fgrep -f reservedList.txt yourList > /dev/null 2>&1 ; then
    echo "found items in reserved List"
 else
    echo "no items found in reserved List"
 fi

You may need to use grep -F reservedList ..... Use the first example above to see that is working, and if not consult man grep to see which exact arguments to use in your OS/grep versions.

If you really need to do this in awk, you'll basically be remaking this functionality in awk.

I hope this helps.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • Also, what is wrong with the answer GlennJackman gave you in http://stackoverflow.com/questions/7741700/evaluating-command-with-awk ?? You keep asking the same question. Arg! Good luck. – shellter Oct 26 '11 at 14:00
  • Your solution could be good if I had a list of all the reserved address. But I don' have it. Reserved addresses are in great number and their format is shown in the wikipedia page I posted. Also some of them have a CIDR prefix that is not a multiple of 8 so creating a regular expression for them is not so easy – papafe Oct 27 '11 at 08:50
  • 1
    Removed previous comment as it was written very early in the a.m. when I didn't have the time to re-read the address list on wikip (I was wrong ;-( ). Anyway, the very first sentence in your message is 'I have a list of ip adrresses' with no mention until now of reg exprs. You'll solve your problem only when you can spend the time to write a clear description of what you are trying to accomplish, how you want/need to do it, and why you think you need to do it that way. (see next). – shellter Oct 27 '11 at 15:41
  • If you know that 'some of them have a CIDR prefix that is not a multiple of 8 so creating a regular expression for them is not so easy' (and you are correct in that assumption), then it sounds like you have to take the time to figure out your own reg-exp to capture just what you want. Ultimately, you can process your list in awk with a big `if (addr ~ regexp1) { do_this } ; else if (addr ~ regexp2) { do_this2 } else if .... { do_that } ; else { print "no match for "addr "}`. Perl reg-exps are considerably more advanced and may be a better solution. Also consider tagging your post with bash. GL – shellter Oct 27 '11 at 15:43
  • Actually I'm don't know much about reg exp but I think that maybe them could be used to solve this problem. About a possible solution a friend of mine told me that one could be to use some sort of geolocalization service. Because reserved ip address do not "belong to anyone" it will sure return some code of error or similar. – papafe Oct 27 '11 at 18:26
  • See http://www.grymoire.com/Unix/Awk.html . This is a great site about awk. You have to spend some time learning about it. The section about awk patterns will be a big help to you ( http://www.grymoire.com/Unix/Awk.html#uh-47 ). Good luck. – shellter Oct 27 '11 at 19:34
  • 1
    see the answer for http://stackoverflow.com/questions/7933492/regular-expression-in-grep Good luck. – shellter Oct 28 '11 at 19:27