grinder has asked for the wisdom of the Perl Monks concerning the following question:
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"; } }
|
|---|