http://www.perlmonks.org?node_id=88074

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

Hi. I'm writing a little batch pinging utility that asks for the third octet and then scans the fourth from 1-255. I want the ping results to go to a log file, and then to scan that log file for a line containing "x bytes from 10.32.z.y" so that i know that ip is taken. The code is below. First though, I want to make sure my problems are clear: 1. It prints nothing to "ips.txt", even though ping fills out "log.txt" fine. 2. Styllistically, I want any suggestions to make the code more idiomatic.
---------------------------------------------------------------------- +----- #!/usr/bin/perl -w use strict; print "Please enter the third octet for scannin': "; my $octet3 = <>; chomp $octet3; open(LOG, ">log.txt") || die "Couldn't open log file: $! \n"; for(my $x = 0; $x < 20; $x++){ open(PINGIN, "ping 10.32.$octet3.$x -w 5 |") or die "Couldn't pull that one off: $! \n"; while(<PINGIN>){ warn "Logging activity for 10.32.$octet3.$x \n"; print LOG $_; } } close PINGIN; close LOG; open(LOG, "log.txt") or die "$!"; open(UNAVAILABLE, ">ips.txt") or die "$!"; while(my $line = <LOG>){ if($line =~ /^64 bytes from ([0-9.]):/){ my $ip = $1; print UNAVAILABLE "$ip is unavailable.\n"; } } close LOG; close AVAILABLE; --------------------------------------------------------------------
any help would be much appreciated. Thanks, Derek