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/perl
use 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;
}
print "Enter IP/Mask one per line (1.2.3.0/24). End with CTRL+D.\n";
my $cidr =Net::CIDR::Lite->new;
while(<>){
if(/($ipv4String\/[0-9]{1,2})/){
my $item=$1;
$cidr->add($item);
}
else{
print "Ignoring previous line.\n";
}
}
my @cidr_list = $cidr->list;
print "======Aggregated IP list:======\n";
foreach my $item(@cidr_list){
print "$item\n";
}
Here's how to run the script:
root@panopticon variousScripts]# ./aggregateCIDR.pl
Enter IP/Mask one per line (1.2.3.0/24). End with CTRL+D.
1.1.1.0/24
1.1.2.0/24
1.1.4.0/24
1.1.3.0/24
1.1.0.0/24
1.1.1.128/25
======Aggregated IP list:======
1.1.0.0/22
1.1.4.0/24
[root@panopticon variousScripts]# ./aggregateCIDR.pl
The script requires Net::CIDR::Lite. If you get an error message, you need to install this module:
[root@panopticon variousScripts]# ./aggregateCIDR.pl
Can't locate Net/CIDR/Lite.pm in @INC (@INC contains: /usr/lib64/perl5/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./aggregateCIDR.pl line 4.
BEGIN failed--compilation aborted at ./aggregateCIDR.pl line 4.
[root@panopticon variousScripts]# perl -MCPAN -e 'install Net::CIDR::Lite;'
CPAN: Storable loaded ok
Going to read /home/adrianp/.cpan/Metadata
Database was generated on Mon, 03 Sep 2007 07:37:38 GMT
...
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/perl
use 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;
}
print "Enter IP/Mask one per line (1.2.3.0/24). End with CTRL+D.\n";
my $cidr =Net::CIDR::Lite->new;
while(<>){
if(/($ipv4String\/[0-9]{1,2})/){
my $item=$1;
$cidr->add($item);
}
else{
print "Ignoring previous line.\n";
}
}
my @cidr_list = $cidr->list;
print "======Aggregated IP list:======\n";
foreach my $item(@cidr_list){
print "$item\n";
}
Here's how to run the script:
root@panopticon variousScripts]# ./aggregateCIDR.pl
Enter IP/Mask one per line (1.2.3.0/24). End with CTRL+D.
1.1.1.0/24
1.1.2.0/24
1.1.4.0/24
1.1.3.0/24
1.1.0.0/24
1.1.1.128/25
======Aggregated IP list:======
1.1.0.0/22
1.1.4.0/24
[root@panopticon variousScripts]# ./aggregateCIDR.pl
The script requires Net::CIDR::Lite. If you get an error message, you need to install this module:
[root@panopticon variousScripts]# ./aggregateCIDR.pl
Can't locate Net/CIDR/Lite.pm in @INC (@INC contains: /usr/lib64/perl5/5.8.5/x86_64-linux-thread-multi /usr/lib/perl5/5.8.5 /usr/lib64/perl5/site_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/site_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl/5.8.4 /usr/lib/perl5/site_perl/5.8.3 /usr/lib/perl5/site_perl/5.8.2 /usr/lib/perl5/site_perl/5.8.1 /usr/lib/perl5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib64/perl5/vendor_perl/5.8.5/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.4/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.3/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.2/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.1/x86_64-linux-thread-multi /usr/lib64/perl5/vendor_perl/5.8.0/x86_64-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl/5.8.4 /usr/lib/perl5/vendor_perl/5.8.3 /usr/lib/perl5/vendor_perl/5.8.2 /usr/lib/perl5/vendor_perl/5.8.1 /usr/lib/perl5/vendor_perl/5.8.0 /usr/lib/perl5/vendor_perl .) at ./aggregateCIDR.pl line 4.
BEGIN failed--compilation aborted at ./aggregateCIDR.pl line 4.
[root@panopticon variousScripts]# perl -MCPAN -e 'install Net::CIDR::Lite;'
CPAN: Storable loaded ok
Going to read /home/adrianp/.cpan/Metadata
Database was generated on Mon, 03 Sep 2007 07:37:38 GMT
...
Comments