#!/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 = < Ping or Fetch
Ping   Fetch

Enter a number :    

Enter a URL/URI :

Enter Email ID :    

Click Here to View the Log file :


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 "
"; print "Number's Value is : " . $cgi->url_param('Number'); print "
"; print "URL's Value is : " . $cgi->url_param('URL'); print "
"; print "Email's Value is : " . $cgi->url_param('Email'); print "
"; print "
"; $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\@gmail.com"; 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 "
"; print "
"; 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 "
"; if ($count<$number) { $count = $count + 1; &myping; } else { print "
$date"; my $logfile = $logfilelocation . "log.txt"; my $logmsg = "$date cannot ping $newURI"; open LOGFILE, ">>$logfile" or die "cannot open logfile $logfile for append: $!"; 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 => 'abc@gmail.com', password => '1234qwerty', ] } ); eval { $sender->send($email) }; die "Error sending email: $@" if $@ } } } } } }