Thursday, March 13, 2008

I've been searching the internet for a script/program that can automatically summarize CIDR classes that I pass to it. I haven't found one, but I found a perl module (Net::CIDR::Lite) that does just this.

I wrote a small script that takes the CIDR classes and summarizes them and then outputs the summarized classes. This is useful for generating a routing table for a management server and basically you have to copy the classes from a router, summarize them and paste them on the server.

Here's the script - copy/paste it in a perl file and make it executable:

#!/usr/bin/perluse strict;use warnings;use Net::CIDR::Lite;

my $ipv4String='[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}';

if(defined $ARGV[0] && $ARGV[0] eq '-h'){ print "usage: $0

This script summarizes your IP classes (if possible). Input IPs with mask one per line. End with CTRL+D. Optionally, redirect a file to stdin like so: $0 < cidr.txt ";exit;}

Here's how to run the script:root@panopticon variousScripts]# ./aggregateCIDR.plEnter IP/Mask one per line (1.2.3.0/24). End with CTRL+D.1.1.1.0/241.1.2.0/241.1.4.0/241.1.3.0/241.1.0.0/241.1.1.128/25======Aggregated IP list:======1.1.0.0/221.1.4.0/24[root@panopticon variousScripts]# ./aggregateCIDR.pl