Recently upon browsing through some of the code and snippets posted on Perlmonks, I came across a number of nodes which performed network administration tasks. I was however dismayed to find many of these nodes used hard-coded network address, iterating through each host address, in one case, as follows:

my $subnet = "40"; for(1..239){ chomp; my $ip = "10.1.$subnet.$_"; . . }

To my mind, there is a much better way to iterate through network hosts, namely, making use of the Net::Netmask module - The following subroutine of code uses this module to iterate through network hosts, the network determined by the network address and subnet mask passed as arguments to this subroutine. The network address and subnet mask passed to this subroutine can take the form of separate network block and subnet masks (eg. 192.168.1.0 and 255.255.255.0) or CIDR notation (eg. 192.168.1.0/24).

Never again should your network administration scripts be non-portable!

use Net::Netmask; sub ips { my $net = Net::Netmask->new(@_); wantarray ? $net->enumerate : \@{$net->enumerate}; }

In reply to Iterate network hosts by rob_au

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.