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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.