I'm trying to be Lazy and find a module that will iterate through an arbitrary CIDR for me. For example:

172.18.0.0/18
172.19.1.0/24
172.20.2.0/28
172.21.3.128/30

My first impulse was to dig out Net::CIDR and use cidr2octets but that only returns the leading octets representing the netblock in question. E.g., cidr2octets("172.19.1.0/24") returns 172.19.1. So then I turned to CPAN and found Net::IPv4Addr but, at a first brief glance, that doesn't help me. I'm sure there's a module that does what I want, I just don't know its name...

To be precise, I want something that takes something like 172.21.3.128/30 and returns

172.21.3.128
172.21.3.129
172.21.3.130
172.21.3.131

Thanks for any pointers (or code :)

Update: NetAddr::IP is indeed the ticket (so I guess patching Net::CIDR isn't necessary, although I will write to Sam suggesting a SEE ALSO item in the documentation of Net::CIDR).

For those following along at home, given an array of CIDR netblocks in @cidr, the following code will print all the IP addresses. Short, sharp and sweet.

for my $cidr( @cidr ) { print "$cidr\n"; my $n = NetAddr::IP->new( $cidr ); for my $ip( @{$n->hostenumref} ) { print "\t", $ip->addr, "\n"; } }

In reply to Iterating through all IP addresses in a CIDR by grinder

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.