Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I have this Perl CGI script using which i want to ping and fetch files from desired URLs and if a failure occurs the a log file is updated and mailed to desired email addresses.

For this i created a HTML page where the Required values are needed to be inserted.

But the problem is that when the program is unable to Ping the desired URL, the details are neither added into a log file nor the Log file is mailed.

I'm Posting my Script Below.

#!/usr/bin/perl -w use CGI::Ajax; use CGI; use LWP::Simple; use Email::Send; use Email::Send::Gmail; use Email::MIME::Creator; use Net::Ping::External qw(ping); use HTML::Template; sub Show_HTML { my $html = <<EOT; <HTML> <HEAD><title>Ping or Fetch</title> </HEAD> <BODY> <script type="text/javascript"> function CheckIsNumeric() { var AsciiCode = event.keyCode; if ((AsciiCode < 48) || (AsciiCode > 57)) { alert('Please enter only numbers.'); event.cancelBubble = true; event.returnValue = false; } } function display(selector,inputID) { var selector=document.getElementById(selector); var inputID=document.getElementById(inputID); if (selector[selector.selectedIndex].value=="Other") { "block"; } else {"none"; } } </script> <form name="orderform" method="GET" > <input type="radio" name="pick" value="Ping" checked="checked">Ping &nbsp; <input type="radio" name="pick" value="Fetch" />Fetch<br /> <br> Enter a number : &nbsp; &nbsp; <input type="text" name="Number" id="myText" size="6" onkeypress = +"CheckIsNumeric()"/> <br> <br> Enter a URL/URI : <input type="text" name="URL" id="URI" size="25"> <br> <br> Enter Email ID : &nbsp; &nbsp; <input type="text" name="Email" id="Email" size="40"> <br> <br> <input type="submit" value=" Submit " /> </Form> <form name = "Log" method = "POST" Action="http://localhost/~Appy/l +og.txt"> Click Here to View the Log file : <input type="submit" value="Log File" /> <br> <hr> <br> </Form> </BODY> </HTML> EOT return $html; } my $cgi = new CGI(); my $pjx = new CGI::Ajax( 'ping_and_fetch' => \&ping_and_fetch ); print $pjx->build_html($cgi,\&Show_HTML); $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "GET") { &ping_and_fetch(); } else {} sub ping_and_fetch { print "Radio Button's Value is : " . $cgi->url_param('pick'); print "<br>"; print "Number's Value is : " . $cgi->url_param('Number'); print "<br>"; print "URL's Value is : " . $cgi->url_param('URL'); print "<br>"; print "Email's Value is : " . $cgi->url_param('Email'); print "<br>"; print "<br>"; $Ping_or_Fetch = $cgi->url_param('pick'); my $number = $cgi->url_param('Number'); my $URI = $cgi->url_param('URL'); my $Email = $cgi->url_param('Email'); my $from = "abc\"; my $count = 0; my $date = localtime(); my $logfilelocation = "/Users/Appy/Desktop/"; if ($number eq "" || $Email eq "" || $URI eq "" ) { print "Please enter all the values"; } else { print "Thank You"; print "<br>"; print "<br>"; if ($Ping_or_Fetch eq 'Ping') { &myping($URI,$URL); sub myping { my $newURI = $_[0]; my $newURL = $_[1]; my $alive = ping(host => "$newURI", timeout => 15); if ($alive) { print "$newURI is active.\n"; $count = 0; } else { print "$newURI is inactive\n"; print "<br>"; if ($count<$number) { $count = $count + 1; &myping; } else { print "<br> $date"; my $logfile = $logfilelocation . "log.txt"; my $logmsg = "$date cannot ping $newURI"; open LOGFILE, ">>$logfile" or die "cannot open logfile $logfile for ap +pend: $!"; print LOGFILE $logmsg, "\n"; close LOGFILE; my $email = Email::MIME->create( header => [ From => $from, To => $Email, Subject => $logmsg, ], attributes => { filename =>"log.txt", content_type =>"application/text", disposition =>"attachment", Name =>"/Users/Appy/Sites/log.txt" +, }, body => "$newURI is not working" ); my $sender = Email::Send->new( { mailer => 'Gmail', mailer_args => [ username => '', password => '1234qwerty', ] } ); eval { $sender->send($email) }; die "Error sending email: $@" if $@ } } } } } }
Please tell me Why is the code unable to update the log file ?

Kindly Help. Thanx in Advance .

In reply to Perl not updating Log File by Appy16

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-06-16 18:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.