Hello, I am new to Perl and have spent about 15 hours (mostly on google, tutorials and bought a Perl book) trying to figure out what I assumed was a simple thing to do.

I have 3 files: host.file - which has IP addresses and possibly hostnames email.file - which is an output file that is updated when I am emailed about a failed ping. output.file - basically just used for debugging/logging

What I am trying to do is ping a list of hosts/IPs then email myself if the ping fails. So far I can get all of that to work. I run the script like every 5 minutes. The 'problem' is that I do not want it to email me every 5 minutes when it detects a failure, I would like it to add that IP to a list (email.file) then notice that I just emailed it and skip over it a few times, maybe for 60 minutes or something before it clears from the list and is available to be emailed again when the ping fails. I hope that makes sense.

In the code I commented out my failed attempts, and removed most of the Net::SMTP stuff just to clean up the code. The trouble area I assume is around the while (@efile = <eFILE>) { area.

Sorry about the sloppy code, I am new to coding, thanks, Sam.

#!/usr/local/bin/perl -w use Net::Ping; #use Net::SMTP; sub email() { print "emailed: $_\n"; print eFILE ("$_\n"); } open(INFILE, "<", "host.file") or die("unable to open file: $!"); my @ip_array = <INFILE>; chomp(@ip_array); open(OUTFILE, ">", "output.file") or die("unable to write to file: $!" +); open(eFILE, "+<", "email.file") or die("unable to write to file: $!"); my @efile = <eFILE>; chomp(@efile); $p = Net::Ping->new(); foreach (@ip_array) { if($p->ping($_)) { print OUTFILE ("$_ is responding to ping.\n"); }else{ print OUTFILE ("$_ is NOT responding to ping.\n"); #while (@efile = <eFILE>) { #if (@efile =~ $_) { #print "found $_ on the list.\n" #}else{ email() #} #} } } close(INFILE); close(OUTFILE); close(eFILE);

In reply to Trouble with Array or ?? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.