Last active
October 1, 2018 03:58
-
-
Save Pliner/6bd594bc8432d9775188d0eb9d16f25e to your computer and use it in GitHub Desktop.
Merge several lists of the networks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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