tags:

views:

541

answers:

2

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

Thanks.

+5  A: 

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.

kbyrd
+2  A: 

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;
}