On 24/05/2013 21:18, shawn wilson wrote:

How do I find the next subnet? This should print 192.168.1.0 the
second time - it errors:
#!/usr/bin/env perl

use strict;
use warnings;

use Net::IP;

my $ip = Net::IP->new('192.168.0.0/24');

print "Start ip [" . $ip->ip . "]\n";
print "start mask [" . $ip->prefixlen . "]\n";

$ip->set($ip->last_ip);
$ip++;
$ip->set($ip->ip . "/" . $ip->prefixlen);

print "Start ip [" . $ip->ip . "]\n";
print "start mask [" . $ip->prefixlen . "]\n";

Or without Net::IP:

perl -Mstrict -wle'
  my $subnet = $ARGV[0];

  my ($ip, $bits) = $subnet =~ m{(\S+)/(\S+)};  # parse

  my $ip_int = unpack "N", pack "CCCC", split /\./, $ip;
  my $step = 1 << (32 - $bits);

  #$ip_int &= (0xFFFFFFFF ^ ($step - 1));  # normalize
  $ip_int += $step;  # increment

  my $next_ip = sprintf "%vd", pack "N", $ip_int;
  print "$next_ip/$bits";
' 192.168.0.42/24

192.168.1.42/24

(you can activate the normalization if wanted)

--
Ruud


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to