in reply to Next subnet

Subnets aren't really that complicated if you convert them to integers. I haven't really tested this code a lot but it should work for any contiguous netmask and valid subnet/netmask combination. You may want to add some error-checking for that.

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.

-- Time flies when you don't know what you're doing

Replies are listed 'Best First'.
Re^2: Next subnet
by taint (Chaplain) on May 25, 2013 at 18:32 UTC
    Greetings ag4ve,
    I'm not sure if this is something you are looking to code all on your own;
    But either way, Net::CIDR might be of some help here. If your just looking for ideas,
    you can have a look at CIDR.pm itself.

    HTH

    --chris

    #!/usr/bin/perl -Tw
    use perl::always;
    my $perl_version = "5.12.4";
    print $perl_version;