Skip to content

Instantly share code, notes, and snippets.

@Pliner
Last active October 1, 2018 03:58
Show Gist options
  • Save Pliner/6bd594bc8432d9775188d0eb9d16f25e to your computer and use it in GitHub Desktop.
Save Pliner/6bd594bc8432d9775188d0eb9d16f25e to your computer and use it in GitHub Desktop.
Merge several lists of the networks
use strict;
use Net::IP;
my @ips = ();
my $result = shift @ARGV;
for(my $i = 0; $i < @ARGV; ++$i) {
open(my $in, $ARGV[$i]) or warn "Can't open $ARGV[$i]: $!";
while (!eof($in)) {
defined(my $line = readline $in) or die "readline failed for $ARGV[$i]: $!";
my $ip = new Net::IP ($line) or die (Net::IP::Error());
push @ips, $ip;
}
}
@ips = sort { $a->binip() cmp $b->binip(); } @ips;
open(my $out, ">", $result) or warn "Can't open $result: $!";
for(my $i = 0; $i < @ips; ++$i) {
my $a = $ips[$i];
unless(defined($a)) {
next;
}
for(my $j = $i + 1; $j < @ips; ++$j) {
my $b = $ips[$j];
unless(defined($b)) {
next;
}
if($a->last_bin() < $b->binip()) {
last;
}
if($a->overlaps($b) != $IP_NO_OVERLAP) {
undef $ips[$j];
}
}
print $out $a->ip(), "/", $a->prefixlen(), "\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment