Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I don't remember where I found this but there is also some perl code that will do it. I wish they added a comment so I could give them credit. I use it to build block lists for adding null routes on hobby web servers using a few blocklists from around the web and for importing data from BGP AS databases. It keeps my routing table below 300K. It's only for ipv4.

[Edit] I think this might be where I found it [1]

    #!/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";
    }

[1] - https://adrianpopagh.blogspot.com/2008/03/route-summarizatio...


If using Python, there's a nice method too: https://docs.python.org/3/library/ipaddress.html#ipaddress.c...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: