in reply to IP Iterator

Sorry to say it, but... that pretty much sucks.

use Net::Ping; use Net::IP; $|=1; my( $from, $to ) = @ARGV; my $p = Net::Ping->new('icmp',2); for ( my $ip = Net::IP->new( "$from - $to" ) or die "error creating Net::IP object: ".Net::IP::Error(); $ip; $ip++ ) { print "\n checking ip: ", $ip->ip, " "; print "ping success " if $p->ping($ip->ip); } $p->close;
A word spoken in Mind will reach its own level, in the objective world, by its own weight

Replies are listed 'Best First'.
Re^2: IP Iterator
by camlet (Novice) on Apr 17, 2008 at 16:04 UTC
    in this case i really didnt want to use an extra module since this is deployed on a list of servers. and it seems Net/IP.pm is not in the default install.

    camlet@cam-lin-s04 tmp# ./test.pl

    Can't locate Net/IP.pm in @INC (@INC contains:

    [camlet@cam-lin-s04 tmp]# cat test.pl #!/usr/bin/perl use Net::Ping; use Net::IP; $|=1; my( $from, $to ) = @ARGV; my $p = Net::Ping->new('icmp',2); for ( my $ip = Net::IP->new( "$from - $to" ) or die "error creating Net::IP object: ".Net::IP::Error(); $ip; $ip++ ) { print "\n checking ip: ", $ip->ip, " "; print "ping success " if $p->ping($ip->ip); } $p->close;

      If you don't want to use an "extra module" then you can do it like this:

      #!/usr/bin/perl use warnings; use strict; use Net::Ping; if ( @ARGV != 2 ) { print "ipiterator <startip> <endip> \n"; exit 0; } my ( $start, $end ) = map unpack( 'N', pack 'CCCC', split /\./ ), @ARGV; for my $count ( $start .. $end ) { my $ip = join '.', unpack 'CCCC', pack 'N', $count; print "checking ip: $ip"; my $p = Net::Ping->new( 'icmp', 2 ); print ' ping success' if $p->ping( $ip ); $p->close(); print "\n"; }

        for my $count ( $start .. $end ) won't work for IP address ≥ to 128.0.0.0 on a 32-bit system. Use a C-style for loop instead.

        This was already mentioned (along with the methodology you used) in my earlier node.

        its the a,b,c network class increments are show below
        ./ipiterator.pl 17 2.29.140.250 172.29.141.4 checking ip: 172.29.140.250 ping success checking ip: 172.29.140.251 ping success checking ip: 172.29.140.252 checking ip: 172.29.140.253 ping success checking ip: 172.29.140.254 checking ip: 172.29.<b>140</b>.255 checking ip: 172.29.<b>141</b>.0 checking ip: 172.29.141.1 checking ip: 172.29.141.2 ./ipcheck.pl 172.254.254.254 173.0.0.4 checking ip: 172.254.254.254 checking ip: 172.<b>254</b>.254.255 checking ip: <b>173</b>.0.0.0 checking ip: 173.0.0.1 ping success checking ip: 173.0.0.2 ping success checking ip: 173.0.0.3