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

Hi monks, got a new problem... I need to write a subnet mask calculator with perl. The extension Net::IP is really powerful. http://search.cpan.org/dist/Net-IP/IP.pm#ip. There are lots of useful methods available except "first_ip", which I also want to get from the given IP address. Did you guys have some experience on Net::IP and could you please tell me what I should do for getting it? crossposted: http://perlguru.com/gforum.cgi?post=75062;sb=post_latest_reply;so=ASC;forum_view=forum_view_collapsed;;page=unread#unread Thanks in advance!

Replies are listed 'Best First'.
Re: Net IP
by shmem (Chancellor) on May 28, 2013 at 16:27 UTC
    I need to write a subnet mask calculator with perl. The extension Net::IP is really powerful.

    Well, you could use that module. First thing to do is read its documentation, and off you go. Come back with some code to discuss.

    Alternatively I just happen to have a calculator in my toolbox for calculating network, netmask and broadcast out of an IPv4 address in CIDR notation which could be worth being studied by you:

    #!/usr/bin/perl # file cidr $m=pack B32,pop=~'/'x$'; printf"$` network %vd broadcast %vd netmask %vd\n",($z=eval$`)&$m,$z|~ +$m,$m; __END__ Example: cidr 209.197.123.153/27 209.197.123.153 network 209.197.123.128 broadcast 209.197.123.159 netm +ask 255.255.255.224
    Consult the perl documentation for its various operators, perl special variables and internal functions.
Re: Net IP
by t_rex_joe (Sexton) on May 29, 2013 at 14:06 UTC
    It's been easier for me to use "Net::Netmask".
    use Net::Netmask; $range = undef; $range = "$ip/$maskip"; print "-- RANGE: \"$range\"\n"; $msess = undef; $msess = new Net::Netmask($range); $table = undef; $msess->storeNetblock($table); $wireip = undef; $wireip = $msess->base(); $bcastip = undef; $bcastip = $msess->broadcast(); if($debug == 1) { print "WIREIP: \"$wireip\"\n"; print "MASKIP: \"$maskip\"\n"; } # Dump all ips in range $oip = undef; foreach $oip ($msess->enumerate(0)) { print "OIP: \"$oip\"\n"; }
    This has helped with finding ip's in subnets.. Especially When 1000's of subnets are used with different masks.