Light Perl Help Neeed... Please

M

madhadder

Hello All

I have inherated a perl script that checks cisco devices by telneting
to them
and running commands who's output is captured and emailed out. The
problem
I have is that if for whatever reason a device is not reachable the
script dies instead
of continuing with the rest of the devices in the list, and me being a
total Perl novice
I cant figure it out and have given up after 3 days of trying..

What I would like for the script to do is to add a line to the Log file
stating that xxxx
device was not accessable, and to continue with the rest of the
devices. Is this possible?
Below is a sample of the Orig. Unmodified script that does the
checking. It currently works
100%, unless the the device isn't reachable..

Example:

foreach $device(@devlistsslo) {

open (logfile,">>temp/ssl.log");
print logfile "==== $device
============================================\n";

chomp($device);
$prompt=uc($device).">";
$session=telnet->new (Timeout => 20,
Prompt => "/$prompt/");

$session->open($device);
$session->waitfor('/Password:\s/') || die;
$session->print($enablepwd2);
$session->waitfor('/>/') || die;
@output=$session->cmd($cmd12);
print logfile "";
print logfile "";
print logfile "@output ";
close logfile;

# Close session
$session->print("quit");
$session->close;


Thanks
 
U

usenet

I have inherated a perl script...

Your predecesor didn't bother to include any exception handling. Damian
Conway would be shocked! (but not really)

I presume you are using the Net::Telnet module. You can see the docs
for this module at:
http://search.cpan.org/~jrogers/Net-Telnet-3.02/lib/Net/Telnet.pm

Of particular interest to you is the section that reads:

A bit more reading shows that you can pass a value to the telnet object
constructor to override this behavoir; for example:

$session=telnet->new (Timeout => 20,
errmode => 'return',
Prompt => "/$prompt/");

That should get you started... but you may want to have a closer look
at what little error reporting you have (namely the "|| die"
statements, which terminate the program on statement failure, but
without bothering to report the error, which is $session->errmsg). I
presume you are also doing this processing in a loop - you should break
out of the loop iteration (with a next() statement) when an error is
encountered (so your script doesn't blindly continue trying to do
operations against a broken telnet connection). But you didn't show
enough code context for me to ofer a specific recommendation.

Good luck! If you ever become a programmer, remember this and read and
follow Damian Conway's "Perl Best Practices" (O'Reilly Books).
 
T

Tad McClellan

I have inherated a perl script


It is what I would call "crufty" code.

I would not allow it to be installed for production use
in its current shabby state.

Below is a sample of the Orig. Unmodified script that does the
checking. It currently works
100%, unless the the device isn't reachable..


Even a broken clock "works" twice a day.

You just haven't looked at your "clock" at a time where its
problems are evident.

If the logfile cannot be opened, there is not much point in
continuing with your logging program, but yours will attempt
to plow ahead anyway.

If the next version of perl introduces a new function named logfile(),
then your "working" program will stop working.

Since both of those can be fixed by changing just one line of code,
I would conclude that you inherited this from a not-so-good programmer...

foreach $device(@devlistsslo) {


Looks like "use strict" is not turned on. It should be.

The more we look, the less "quality" we see in this code. :-(

open (logfile,">>temp/ssl.log");


You should always, yes *always*, check the return value from open().

You should use UPPERCASE filehandle names (or even better, use
lexical filehandles instead).

open LOGFILE, '>>temp/ssl.log' or die "could not open the logfile: $!";
or
open my $logfile, '>>temp/ssl.log' or die "could not open the logfile: $!";


print logfile "==== $device
============================================\n";

chomp($device);


(I assume that was supposed to be one line of code, but it got
wrapped by your "newsreader".)


Do you _want_ a newline in the middle of that output?

If not, then it is too late to chomp() the newline after the
output has already been made.

$prompt=uc($device).">";


$prompt = "\U$device>"; # easier to read and understand?

print logfile "";
print logfile "";


What purpose could those 2 lines possible have?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,109
Latest member
JanieMalco
Top