Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to increase the network address of a subnet, rather than increasing the ip address

ex: 20.20.20.1/24, the next should be 20.20.21.1/24, 20.20.255.1/24, 20.21.20.1/24 and so on. I have user NetAddr::IP, which only increments ip address. Is there any function to increment the network address

my $ip_add = NetAddr::IP->new('20.20.20.1/24'); for (my $ip = 1; $ip<=2000; $ip++) { my $iponly = (split '/', $ip_add)[0]; print "$iponly\n";

the above code prints from 20.20.20.1- 20.20.20.255

Replies are listed 'Best First'.
Re: Increasing the network space in IP
by salva (Canon) on Jan 28, 2016 at 09:19 UTC
    Is there any function to increment the network address

    You can always do it yourself:

    # untested! sub net_inc { my $n = shift; my $inc = (1 << (32 - $n->masklen)); return $n + $inc }
Re: Increasing the network space in IP
by poj (Abbot) on Jan 28, 2016 at 08:56 UTC

    Maybe Net::Netmask

    #!perl use strict; use Net::Netmask; my $block = new Net::Netmask('20.20.20.0/24'); for (1..10){ print $block->nth(1)."\n"; $block = $block->nextblock(1); }
    poj