Noosrep Noosrep - 1 year ago 85
Perl Question

Loop over a range of IPs with perl IP library

I'm trying to create an IP table list where I can see which IPs are in use and which aren't.

I use the

library for this. Code snippet:

my @IPsinrange = &getAllIPs($range) ; #range is x.x.x.x/subnet format
sub getAllIPs {
my $ip = new Net::IP ($range) || die;
my @IPs ;
# Loop
do {
push @IPs, $ip->ip() ;
} while (++$ip);

return @IPs ;

This works for a x.x.x.0/24 network and this works for but when I use for instance, the loop just dies. I thought it could be because of the fact that there were no IP adresses in use, but there are 4 IPs in that range that are alive.

Answer Source

The reason this isn't working is due to the fact that the module requires you to send in a proper network address as the starting point of a given IP prefix.

Since you want to use a /29 prefix, valid ranges would be:

As mentioned in the comments, the documentation states that the proper usage of the constructor to get proper diagnostic output is:

$ip = new Net::IP ('') or die (Net::IP::Error());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download