I have a file containing the output after pinging several IP addresses. For example, the first line is "12.34.56.78 is alive" and line 2 is "no answer from 12.34.56.79" and so on. I am trying to print only those lines that contain the words "is alive." I tried several things including grep and if-else statements but only want those lines containing "is alive" to print to a separate file & am having no luck. Heres what I have so far:
# Create a list of all possible IP addresses within subnet
open(ALLIP, ">allip.list") || die "Unable to create file: allip.list\n
+";
for ($i=0; $i<=20; $i++) {
print ALLIP "$ip_subnet.$i\n";
}
close(ALLIP);
open(ALLIP, "allip.list") || die "Unable to open file: allip.list\n";
open(PINGALLIP, ">pingallip.list") || die "Unable to open file: pingal
+lip.list";
while (<ALLIP>) {
chomp;
system("ping $_ >>pingallip.list") && "Unable to create file:
+pingallip.
list";
}
close(ALLIP);
close(PINGALLIP);
open(ALIVEIP, ">aliveip.list");
close(ALIVEIP);
open(ALIVEIP, "aliveip.list");
open(PINGALLIP, "pingallip.list") || die "Unable to open file: pingall
+ip.list";
while (<PINGALLIP>) {
if (/is alive/) {
chomp;
print ALIVEIP $_;
}
}
close(PINGALLIP);
Any help would be greatly appreciated. The idea of using the IP module has already been mentioned to me but I am also using this as an excuse to improve my perl skills. =) Thanks.
Zuinc
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.