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.

1 Comment

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 comment

Recent Entries

  • flashpolicyd 2.0

    I wrote a multi threaded server for Adobe Flash Policy requests, some background from Adobe:Since policy files were first introduced, Flash Player has recognized /crossdomain.xml...

  • Adventures with Ruby

    Some more about my continuing experiences with ruby, in my last post I saidthe language does what you'd expect and as you'll see in my...

  • New programming language of choice - Ruby

    I have fallen out of love with Perl some time ago, I cannot point to one specific thing about it that put me off, I...

  • On working from home

    I've not been posting much here, work has been incredibly manic the last while, especially I need to still finish off my SSO posts with...

  • Rework of puppet facts for /etc/facts.txt

    Previously I blogged a custom fact that reads /etc/facts.txt to build up some custom facts for use in Puppet manifests, well I've since learned a...

Close