#!/usr/bin/perl -w
#==========================USAGE======================================
# Usage: $0 URL
# Options: -p <proxy> Proxy Server, e.g. http://proxy.dlh.st.com:8080
# -u <user> Proxy Server username
# -a <auth> Proxy Server authentication
# -t <sec> Timeout in seconds (default is 60 secs)
# -d <debug> Bebug code level
#
#e.g. perl web.pl http:\\xxx.yy.zz.com (for intranet sites)
#=====================================================================
+=
#======================Perl module used===============================
+===
#used Getopt::Long also for parsing the command line parameters
use LWP;
#=====================================================================
+===
#======================Script Specific Variables======================
+===
#$recient is the person you want to send the message to
#$list is the list of people in cc
#alerts can be sent to multiple people
my $recipients = "";
my $list = "";
my $sender = "";
#=====================================================================
+===
my($DEBUG,$proxy,$user,$pwd,$timeout,$VPO)= &get_options;
#script works with or without proxy access depending on your environme
+nt.
#The C<LWP::UserAgent> is a class implementing a simple World-Wide Web
#user agent in Perl.
#The request() method can process the content of the response in one o
+f
#three ways: in core, into a file, or into repeated calls to a
#subroutine. You choose which one by the kind of value passed as the
#second argument to request().
#The in core variant simply stores the content in a scalar 'content' a
+ttribute
#of the response object and is suitable for small
#HTML replies that might need further parsing
if ($proxy eq '')
{
print " No Proxy Server specified\n" if ($DEBUG);
$proxy='<noproxy>';
$ua=LWP::UserAgent->new;
$ua->timeout($timeout) if(defined($timeout));
$req=HTTP::Request->new(GET=> $url);
$res= $ua->request($req);
print "status=",$res->status_line,"\n" if($DEBUG);
print "is_success=",$res->is_success,"\n" if($DEBUG);
}
else
#if a proxy server is specified then we use a http GET
{
print "Using $proxy as the proxy server \n" if ($DEBUG);
$ua=LWP::UserAgent->new;
$ua->proxy('http' => $proxy);
$ua->timeout($timeout) if(defined($timeout));
$req=HTTP::Request->new(GET=> $url);
$req->proxy_authorization_basic($user,$pwd) if($user ne '' && $pwd ne
+ '');
$res= $ua->request($req);
print "status=",$res->status_line,"\n" if($DEBUG);
print "is_success=",$res->is_success,"\n" if($DEBUG);
print "GET result=",$res->as_string,"\n" if($DEBUG>1);
}
#checking if the result of the request was successful or not
if($res->is_success)
{
print "passed \$res->is_success\n" if ($DEBUG);
print "$url,$proxy,SUCCESS\n";
exit 0;
}
else
#if it is no successfult you can either call a mailing function with r
+ecipent,cc,sender,subject,message as parameters
{
$err=$res->status_line;
$notify_err="$url,$proxy,FAILED,ERROR:$err";
print $notify_err."\n";
#======================Mail specific varables=========================
+=====
my $sub = "URL $url is not available";
my $msg = "URL $url is not available \n\n PROXY SERVER : $proxy \
+n\n STATUS = FAILED \n\n ERROR : $err\n\n Regards.";
#=====================================================================
+=====
sendmail($recipients,$list,$sender,$sub,$msg);
}
exit 0;
#=================Sub routine to parse the command line parameters====
+=============
#Default timeout has been specified as 60 seconds
#checking for blank proxy or username password is done
#Set code level to 1 ( -d 1) see usage if you want to see the options
+taken in from the command line
#=====================================================================
+=============
sub get_options {
my($DEBUG,$Proxy,$proxy,$user,$pwd,$timeout,$VPO);
$proxy=$Proxy=$user=$pwd='';
$timeout=60;
$DEBUG=0;
$VPO=0;
use Getopt::Long;
GetOptions ( "d=i" => \$DEBUG,
"p=s" => \$proxy,
"u=s" => \$user,
"a=s" => \$pwd,
"t=i" => \$timeout
);
print "proxy = $proxy ,user = $user pwd = $pwd \n" if($DEBUG);
$url = $ARGV[0] || &usage;
if( ( $user ne '' && $pwd eq '' ) || ( $pwd ne '' && $user eq ''))
{
print STDERR "If you specify a user id (-u) for the proxy se
+rver ,you must also specify an authorization password (-a) and vice-
+verca\n";
&usage;
exit 1;
}
if(($pwd ne '' || $user ne '') && ($proxy eq '' && $Proxy eq ''))
{
print STDERR "You specified user/password but no proxy server.
+\n";
print STDERR "The user/password info will be ignored.\n";
}
if($DEBUG)
{
print "get_options:DEBUG=$DEBUG\n";
print " proxy=$proxy, user=$user, pwd=$pwd\n";
print " timeout=$timeout (sec)\n";
print " url=$url\n";
}
return($DEBUG,$proxy,$user,$pwd,$timeout,$VPO);
}
#========================Subroutine to send alert vie Sendmail========
+==========
#sends a mail to the id in $recipients with cc to the id's in $list
#=====================================================================
+==========
sub sendmail
{
my($recipients,$list,$sender,$sub,$msg) = @_;
open(MAIL, "|/usr/lib/sendmail -t");
$old=select(MAIL) ;
print<<"EOF" ;
To: $recipients
CC: $list
From: $sender
Subject: $sub
Body:
$msg
EOF
close(MAIL);
select($old);
}
#=======================Sub routine to show usage of the script=======
+======================
#notice absolute path names for both the web page to be checked as wel
+l as the proxy server
#=====================================================================
+=======================
sub usage {
print "Usage: $0 URL \n";
print <<'EOF';
URL to be retrieved , e.g. http://www.yahoo.com
Options: -p <proxy> Proxy Server, e.g. http://proxyabc.dlh.st.c
+om:8080
-u <user> Proxy Server username
-a <auth> Proxy Server authentication
-t <sec> Timeout in seconds (default is 60 secs)
-d <debug> Bebug code level
Prints URL ,Proxy Server (if used),Status
Alert Notification scheme depends on the -m switch
Sets exit code to 1 on error and 0 on success
EOF
exit 0;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.