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.

In reply to Re: finding netmask for "arbitrary" ip address by Plankton
in thread finding netmask for "arbitrary" ip address by jasonl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.