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

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

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);