Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: finding netmask for "arbitrary" ip address

by Plankton (Priest)
on Nov 29, 2011 at 06:12 UTC ( #940536=note: print w/ replies, xml ) Need Help??


in reply to finding netmask for "arbitrary" ip address

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.


Comment on 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://940536]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-07-10 21:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (216 votes), past polls