I am not sure I understand the problem you are trying to solve, but if you just want to figure out if a given ip address is within a subnet it is easier if you convert the ip address, and network addresses and broadcast addresses from a quad 4 representation to an integer. Then do something like this ...
#!/usr/bin/perl -w
use Socket;
use strict;
my %subnets = (
'10.96.2.0/255.255.254.0' => {
start => 174064128,
end => 174064639 }
, '10.123.50.0/255.255.255.0' => {
start => 175845888,
end => 175846143 }
, '72.24.196.0/255.255.255.0' => {
start => 1209582592,
end => 1209582847 }
, '72.24.137.192/255.255.255.192' => {
start => 1209567680,
end => 1209567743 }
, '10.122.50.0/255.255.255.0' => {
start => 175780352,
end => 175780607 }
, '65.181.207.128/255.255.255.128' => {
start => 1102434176,
end => 1102434303 }
);
my $ip = shift;
my $int = unpack("N",inet_aton(shift||$ip));
for my $subnet ( keys %subnets ) {
if ( $subnets{$subnet}{'start'} <= $int &&
$int <= $subnets{$subnet}{'end'}
) {
print "$ip ($int) is in subnet $subnet (" .
$subnets{$subnet}{'start'} .
", " . $subnets{$subnet}{'end'} .
") \n";
}
}
I used ipcalc to work out the network address and broadcast address and script I wrote, quad2int.pl, to convert those IP to integer.