Calculating CIDR notation from IP ranges

Following from my previous post about blocking some more countries I discovered a bit of a short coming in the code I used to calculate CIDR notation from ranges of IP’s. So a bit of searching on CPAN got me Net::CIDR.

use Net::CIDR;
$range = shift;
print (join(“\n”, Net::CIDR::range2cidr(“$range”)) . “\n” );

This will take any given range of ip address in format a.b.c.d-w.x.y.z and spew out a list of subnets required to cover the whole range:

# ./range2cidr.pl 64.139.147.0-64.139.170.255
64.139.147.0/24
64.139.148.0/22
64.139.152.0/21
64.139.160.0/21
64.139.168.0/23
64.139.170.0/24

So with this I now have hopefully a more accurate set of rules that will not block bits of New Zeeland as well by accident.

Tags: , ,

One Response to “Calculating CIDR notation from IP ranges”

  1. dan 06. May, 2007 at 03:27 #

    Thanks for this.. I had an example of this someplace, and couldn’t find it… And was googeling for another example. Lol, good find on my part. Excellent help tip on yours.
    dan

Leave a Reply