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

I have a script that pings a URL and mails if there are particular no. of failures. The URLs and the no. of failures before which the email has to be sent are specified in a text file.

What i want to do is next to the URLs i want to specify the Email addresses to which the failures are to be mailed. How can i read them from the text file itself.

Also I want to create a log file after a certain no. of failures. How can I do that ?

Mailing is not a problem as i can use Email::Send::Gmail for it. I am posting my script below. Kindly Help. Thanx in advance.
#!/usr/bin/perl use warnings; use LWP::Simple; use Email::Send; use Email::Send::Gmail; use Email::MIME::Creator; use Net::Ping::External qw(ping); use Tie::File; my $testfolder = "/Users/Appy/Desktop/"; my $to = "test1\@gmail.com"; my $from = "test2\@gmail.com"; my $subject = "Uris not responding."; my $count = 1; tie @file, 'Tie::File', $testfolder . "testfile.txt" or die; foreach $URL (@file) { my $string1 = $URL; if ( $string1 =~ m/ # Match ping\s # 'ping ' /ix # i = Ignore case # x = allow the regexp to go over multiple line +s ) { my $URI = substr $URL, 8; print "$URI\n"; sub myping { my $alive = ping(host => "$URI"); if ($alive) { print "$URI is active.\n"; } else { my $c = substr $URL, 5, 2; $count = $count + 1; if ($count <=round($c/2)) { &myping; } if ($count=$c) { ##create a log file } } }
Testfile

ping 07 abc.in a@b.com,c@d.com

ping 05 google.co.in e@f.com,g@h.com

s include the respective email addresses to which notification is to be sent. and the numbers specify the no. of failures after which the email has to be sent

Replies are listed 'Best First'.
Re: Reading Email List from a text file
by roboticus (Chancellor) on Mar 25, 2010 at 10:23 UTC

    Appy16:

    Try reading open, and pay attention to the examples section, there's an example of reading the file line-by-line in a loop using the <> operator.

    ...roboticus

Re: Reading Email List from a text file
by jethro (Monsignor) on Mar 25, 2010 at 11:47 UTC
    If I understand your question correctly, you need split() to get at the elements of your configuration lines. split can transform a line of space-separated texts into an array with the texts as array contents.

    Just read the man-page (execute "perldoc -f split"), it has lots of examples