in reply to Next subnet
use Socket; sub next_subnet { my ($network, $subnet) = @_; my $inet = unpack('N', inet_aton($network)); my $mask = unpack('N', inet_aton($netmask)); my $ones = unpack('N', inet_aton('255.255.255.255')); my $size = ($ones ^ $mask) + 1; my $next = $inet + $size; return inet_ntoa(pack('N', $next)); }
You may find this handy too:
sub prefix_to_mask { my ($bits) = @_; return inet_ntoa(pack('N', (2**32) - (2**(32-$bits)))); }
Converting from netmask to prefix length is left as an exercise :-)
Edit: I almost forgot, you have to use Socket; for inet_aton/inet_ntoa.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Next subnet
by taint (Chaplain) on May 25, 2013 at 18:32 UTC |