-1

I have two lists. One list with networks (e.g. 10.0.0.0/8 etc.) and a second list with subnets (e.g. 10.10.1.0/24 or /27 /28 etc.). I would like to figure out, which of the subnets fit in the first 'container'-network (using perl).

Any ideas?

Stef
  • 593
  • 3
  • 10
  • 23

2 Answers2

4

The Net::CIDR::Compare package on CPAN is probably what you are looking for. From the example code:

use Net::CIDR::Compare;

my $collection = Net::CIDR::Compare->new(print_errors => 1);

my $first_list = $collection->new_list();
$collection->add_range($first_list, "10.10.0.0/16", 1);

my $second_list = $collection->new_list();
$collection->add_range($second_list, "10.10.200.0/24", 1);

$collection->process_intersection(expand_cidr => 8);
while (my $cidr_range = $collection->get_next_intersection_range()) {
    print "$cidr_range\n"; # prints 10.10.200.0/24
}
Matthew Franglen
  • 4,441
  • 22
  • 32
2

Check NetAddr::IP

use NetAddr::IP;

my $range1 = NetAddr::IP->new("10.0.0.0/8");
my $range2 = NetAddr::IP->new("10.10.1.0/24");
print "range1 contains range2\n" if $range1->contains($range2);
mpapec
  • 50,217
  • 8
  • 67
  • 127