Question

How can I check if IP numbers are in the same subnet with Perl? Do I use NetAddr::IP?

Thanks.

Was it helpful?

Solution

Seems like the Net::Subnets Perl module would do this. There's an example at that link I gave. If you don't want to use CPAN to get the module, both Debian and Ubuntu have a libnet-subnets-perl package.

OTHER TIPS

You could try this:

#!/usr/bin/perl

use warnings;
use strict;
use NetAddr::IP;

my $first  = NetAddr::IP->new('10.0.0.1/24');
my $second = NetAddr::IP->new('10.0.0.5/32');

if ($second->within($first)) {
    printf "%s is within %s\n", $second, $first;
} else {
    printf "%s is not within %s\n", $second, $first;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top