Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

SOLVED Re: finding netmask for "arbitrary" ip address

by jasonl (Acolyte)
on Dec 07, 2011 at 16:35 UTC ( #942278=note: print w/ replies, xml ) Need Help??


in reply to finding netmask for "arbitrary" ip address

Here's what I ended up doing; it does require Socket.pm, but from what I've seen so far that looks pretty standard. It uses inet_aton to convert ip, net, and mask to structs, &s them to compare, then returns the mask when it finds a hit.

use Socket; sub findMask { my ($ip, $nets) = @_; my ($net, $mask) = (); foreach my $i (@$nets) { ($net, $mask) = split(m[/], $i); my $netBin = inet_aton($net); my $maskBin = inet_aton($mask); my $maskedNet = ($netBin & $maskBin); if (((inet_aton($ip)) & $maskBin) eq $maskedNet) { return "$mask"; } } ## no match return -1; }


Comment on SOLVED Re: finding netmask for "arbitrary" ip address
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://942278]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (12)
As of 2015-07-07 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls