'How can I check if IP numbers are in the same subnet with Perl?

How can I check if IP numbers are in the same subnet with Perl?

Can I use NetAddr::IP?



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1