Moniker | 2
Moniker
The Code
So, let's dive right into the code. Lines 3-6 load our modules. Getopt::Std is one module I haven't mentioned yet. It's included with the standard distribution of Perl and handles command line arguments so you don't have to. A nifty little package indeed.
Line 9 sets the number of seconds we'll wait to get
a response from the server we're testing. In line 10,
the email address that will be set in the From: field of the email is assigned to the
$monikeraddr
variable. Line 11
sets the $smtpserver
variable which holds the hostname of the smtp server
we'll send email alerts through.
Lines 13-22 build a multi-dimensional hash that contains the service name, port, the string that's printed once a connection has been established, and the string that we expect from the remote service.
Next, we move into the main body of the code. First we initialize the hash that will hold
the value of the command line switches (line 25).
Then, we call getopt
, which was imported from the Getopt::Std
module (line 26).
The next thing we do in lines 27-30 is check the command-line arguments to make sure values were specified for the -h (host) and -s (service). If they weren't, we remind the user of the syntax. We also validate the service they specified to make sure it's listed in our services hash.
In lines 32-45 we set several lexically scoped
variables (lines 32-37),
create a new instance of the Net::Telnet
module
(line 36), and create a connection to the remote
host (lines 40-45). You'll notice that this last
section of code is surrounded by an eval
. What eval
does in this case
is catch an error, if one occurs when $sock->open
is called on
line 41, and puts the error code or message
into the $@
variable. Then, on line 48,
we execute the &alert
subroutine if $@
contains a value. If
an error does not occur, we can assume that connection has been established with the remote
service.
On line 51, we print the string that's defined
in our services hash (lines 13-22) if it exists.
This is necessary for services like www where the server expects some input before it give
us some output. In the case of HTTP, we send it the string
HEAD / HTTP/1.0\n\n
which requests the HEAD information of the root document
on the Web server.
Lastly, we wait for the string specified in the services hash that relates to the particular
service we're testing (line 54).
In the case of the www service, we're looking for the string
200
which the server returns to let us know the request was successful.
if all goes well, we print the string Service is operational.
on
line 56.
Lines 59-82 contain the alert
subroutine which sends an email to the address specified by the -e
switch on the
command-line. This switch is optional, so if it's omitted, the error is simply printed to
STDOUT. To send the email, we used the sendmail
function which was imported
from the Mail::Sendmail
module. We simply pass it the email header values and
the function does the rest. The nice thing about this module is that it doesn't require
sendmail. It's an completely independent implementation of the SMTP protocol, so you
can send the email through any SMTP server you like.
Produced by Jonathan
Eisenzopf and
Created: August 4, 1999
Revised: August 4, 1999
URL: https://www.webreference.com/perl/tutorial/6/