CIDR cleanup

John Kristoff jtk at depaul.edu
Thu Oct 1 21:29:11 UTC 2020


On Thu, 1 Oct 2020 13:32:53 +0000
John Von Essen <john at essenz.com> wrote:

> I tried to write my code to do this, and its not trivial, just
> lookinh for a shortcurt. I did a breif glance at some CIDR related
> Perl cpan modules, and nothing has jumped out.

I wrote the code below some time ago.  I've not used it in awhile, but
presumably it does what I think it does.  I called it compactaddrs.pl.
Feed it a list of CIDR blocks via stdin.

John

#!/usr/bin/perl -T
use strict;
use warnings;

# compactaddrs - aggregate addr blocks

use NetAddr::IP qw( Compact );

my @v4blocks;
my @v6blocks;

while( defined(my $line=<>) ) {
    chomp $line;

    if ( $line =~ /:/ ) {
        push @v6blocks, NetAddr::IP->new($line);
    }
    else {
        push @v4blocks, NetAddr::IP->new($line);
    }
}

if ( scalar @v4blocks > 0 ) {
    print "# IPv4 aggregate prefixes\n";

    my @aggregates = Compact(@v4blocks);
    print 'WHERE';
    for my $prefix (@aggregates) {
        print ' saddr <<= ';
        print "'$prefix' OR";
#        print "$prefix\n";
    }
}

if ( scalar @v6blocks > 0 ) {
    print " #IPv6 aggregate prefixes\n";

    my @aggregates = Compact(@v6blocks);
    for my $prefix (@aggregates) {
        print "$prefix\n";
    }
}

print "\n";


More information about the NANOG mailing list