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

I want to display all ips within a range of ips. I tried looking at Net::IP and it seemed to have what I wanted, but ofcourse I was unable to get it to work. Here is the code from the docs that I tried.
use Net::IP; my $ip = new Net::IP ('195.45.6.7 - 195.45.6.19') || die; # Loop do { print $ip->ip(), "\n"; } while (++$ip);
This prints the first IP, but then gives me the following error:
"Cant call "ip" method without a package or object reference."

The ++ operator is supposed to return undef when the last address of the range is reached. Any help will be greatly appreciated.

Replies are listed 'Best First'.
Re: get all ips within a range
by tachyon (Chancellor) on Sep 16, 2003 at 17:47 UTC

    This is how to do it going back to basics....

    my $ips = range2list( '195.45.6.7', '195.45.6.19' ); print "$_\n" for @$ips; sub range2list { use Socket; my ( $begin_ip, $end_ip, $max_range ) = @_; $max_range ||= 255; my $s_ip = inet_aton($begin_ip); my $e_ip = inet_aton($end_ip); my $s_dec = unpack 'N', $s_ip; my $e_dec = unpack 'N', $e_ip; # tolerate incorrect range order and flip around ( $s_dec, $e_dec ) = ( $e_dec, $s_dec ) if $s_dec > $e_dec; my $range = $e_dec - $s_dec; $range = $max_range if $range > $max_range; my @ip_list; for ( 0..$range ) { my $ip = pack 'N',$s_dec; push @ip_list, inet_ntoa($ip); $s_dec++; } return \@ip_list; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: get all ips within a range
by Anonymous Monk on Sep 16, 2003 at 15:29 UTC
    Seems to work for me
    use Net::IP; my $ip = new Net::IP ('195.45.6.7 - 195.45.6.19') || die; # Loop do { print $ip->ip(), "\n"; } while (++$ip); __END__ 195.45.6.7 195.45.6.8 195.45.6.9 195.45.6.10 195.45.6.11 195.45.6.12 195.45.6.13 195.45.6.14 195.45.6.15 195.45.6.16 195.45.6.17 195.45.6.18 195.45.6.19
      I got it to work on my linux box using Net::IP-1.20. Before I was trying it on windows using version 1.16, which didn't work. I believe that is the only available version through ppm. Did you get it to work on windows or linux?
        Windows. See Tutorials if you don't know how to install modules.
Re: get all ips within a range
by fokat (Deacon) on Sep 19, 2003 at 02:52 UTC

    <plug type="shameless">You can also give NetAddr::IP a try. There's a tutorial I wrote that teaches how to do this and much, much more.</plug>

    Best regards

    -lem, but some call me fokat

Re: get all ips within a range
by Anonymous Monk on Sep 16, 2003 at 17:10 UTC
    I get the same error. I suspect you're using an old version of Net::IP, which doesn't overload the + operator. There doesn't seem to be any other way of printing the range, either. I can only suggest upgrading to the latest version from CPAN, then the code from the docs should work.
A reply falls below the community's threshold of quality. You may see it by logging in.