#!/perl/bin/perl
#----------------------------------------------------------------------#
# This is a Freeware #
# #
# Perl name : checkhttp.pl #
# Version : 2.0 #
# Programmers : Eli Ofek #
# #
# Description: Report on http status, and send it using SMTP #
# This is perl, v5.6.0 built for MSWin32-x86-multi-thread#
# Invocation: #
# Please send filename , email address, smtp-mailserver #
# The filname will contain urls like: http://www.cimatron.com #
# you can also send specific pages.
# #
# #
#----------------------------------------------------------------------#
# Modification history: #
# #
# Date Programmer Description #
# #
# 2001/08/17 Eli Ofek New program #
# 2001/08/20 Eli Ofek adding option to send mail #
# only if dead ur's were found.#
# #
# YYYY/MM/DD Your name here Short description of changes#
#----------------------------------------------------------------------#
#----------------------------------------------------------------------#
require LWP::UserAgent;
use Net::SMTP;
my $DeadReport;
my $AliveReport;
my $from = 'Check_HTTP_Reporter@EliOfek.co.il';
if ($#ARGV <3) { die "Not enough parameters, Please send filename of urls , email address, smtp-mailserver, (All/Dead) ,[proxy-server ,proxy-port( default 8080) - if needed] \n\n";}
# Get Params
my $filename = $ARGV[0];
my $email = $ARGV[1];
my $mailserver = $ARGV[2];
my $whentosend = $ARGV[3]; # gets "All" or "Dead"
my $proxyserver = $ARGV[4];
my $proxyport = $ARGV[5];
if ($proxyport eq '' ) { $proxyport = "8080";}
# Get Url's
open (URLS,$filename) || die "Cannot open $filename \n\n";
my @host = <URLS>;
close(URLS);
print "Starting to HTTP...\n\n";
# Create a new HTTP object:
$ua = LWP::UserAgent->new;
if (not $proxyserver eq '')
{
$proxydetails = join('','http://',$proxyserver,':',$proxyport,'/');
$ua->proxy(['http', 'ftp'], $proxydetails); # using proxy
}
# else
# {
# $ua->no_proxy(); # using proxy
# }
foreach $host (@host)
{
chomp($host);
if ($host eq '') {next;} # Skip empty url's.
print "\nHTTPing $host ... ";
$request = HTTP::Request->new('GET', $host);
$response = $ua->request($request); # Check
if (($response->is_error)) # Check result
{
$ErrorNo = $response->code;
$ErrorMsg = $response->message;
print "$host is dead., Details: ErrorNo: $ErrorNo , Message: $ErrorMsg \n";
$DeadReport = join("\n",$DeadReport,"$host is dead., Details: ErrorNo: $ErrorNo , Message: $ErrorMsg \n");
}
else
{
print "$host is alive.\n";
$AliveReport = join("\n",$AliveReport,"$host is alive.\n");
}
}
print "\n\nFinished to HTTP...\n\n";
# Check if needs to send a report:
if (not ($DeadReport eq '') or ($whentosend eq 'All'))
{
# Send report using smtp:
print "Sending report to $email ...\n\n";
# Constructor
$smtp = Net::SMTP->new($mailserver);
# Compose mail:
my $body = "Dead urls:\n------------\n\n$DeadReport\n\nAlive urls:\n------------\n\n$AliveReport\n\n";
$smtp->mail($ENV{USER});
$smtp->to($email);
$smtp->data();
$smtp->datasend("From: $from\n");
$smtp->datasend("To: $email\n");
$smtp->datasend("Subject: CheckHTTP Reporter url/s Report.\n");
$smtp->datasend("\n");
$smtp->datasend($body);
$smtp->dataend();
$smtp->quit;
print "Report was sent to $email ...\n\n";
}
print "Done.\n\n";
exit(0);