Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Trapping errors in Net::Telnet::Cisco

by kc6ovd (Acolyte)
on Jun 06, 2011 at 18:16 UTC ( [id://908339]=perlquestion: print w/replies, xml ) Need Help??

kc6ovd has asked for the wisdom of the Perl Monks concerning the following question:

My first post, It has been 8 years since I regularly coded and did sysadmin. I am very rusty. Something new I am working with is this telnet cisco perl mod. I have tried a few things to catch errors but I am missing the mark. Attached is my code, and I have issues durring the loop through the host list. If I get a timeout error from the FTP transfer it dies and does not return and continue. This should go through the list of hosts do the file transfer and log any that fail to error log. but it should get through the list.
#!/bin/perl # # # Writen and Produced By Kevin King #--------------------------------------------------------------------- +---------- # CiscoBackup version 1.00 _ first version ready for use. Needs advanc +ed # error checking. -kk1051 2/16/2011 # v1.01 added Error check for reachable node. -kk1051 2/17/2011 # v1.02 added ping to backuphost to wakeup sleepy OAM. Also added chec +k # for enable mode. -kk1051 2/18/2011 # Had to remove ping check due to a site having ping blocked. -kk1051 +2/18/2011 # use Net::Telnet; use Timestamp::Simple qw(stamp); use Net::Telnet::Cisco; my $dte = stamp; my $backup_host = "xymon"; my $dump_log = "$dte-dump.log"; if ($#ARGV < 2) { print STDERR "USAGE: $0 <User ID> <Password> <hostfile +>\n"; exit; } ### Prepare error.txt as empty file ### open ( ERROR_LOG, ">$dte-error.txt") or die "Could not open $dte-error +.txt.\n"; close ERROR_LOG; $uid = $ARGV[0]; $pawd = $ARGV[1]; $hostfile = $ARGV[2]; ####lets try and set a global log before the foreach loop. open HST, "$hostfile" || die "Couldn't open hostfile: $hostfile.\n"; @iplist = <HST>; foreach $entry (@iplist) { chomp ($entry); &SDM } sub SDM { print "Now trying to connect to $entry\n"; my $session = Net::Telnet::Cisco->new(Host => $entry, Dump_lo +g => $dump_log); if ( $session ) { ### Login to privileged exec mode ### $session->login( Name => $uid , Password => $pawd ); # $session->enable( $pawd ); ### added check for enable mode. Need to clean up and make err +or subrutine### if ($session->enable($pawd) ) { @output = $session->cmd('show privilege'); print "My privileges: @output"; } else { warn "Can't enable: " . $session->errmsg; return; } ### Get device name and store in $device_name ### my @output = $session->cmd( String => "show run | include hos +tname ", Timeout => "30" ) ; $device = substr ( $output[0], 9, length ( $output[0] ) - 10 ) +; $session->cmd(String =>"copy run ftp://$backup_host/backup/rou +ter/$dte-$device.cfg\n\n\n ",Timeout => "120", Errmode => "return"); print "errmsg: " . $session->errmsg . "\n"; $session->close; print "$entry $device OK\n"; } ### If the device was not rechable ### elsif ( ! $session ) { open ( ERROR_LOG, ">>$dte-error.txt") or die "Could not open $ +dte-error.txt.\n"; print "$entry Device was not reachable !\n"; print ERROR_LOG "$entry Device was not reachable !\n"; } } #--------------------------------------------------------------------- +---------# #######lets clean up some of the files##### if (-z "$dte-error.txt" ){ `rm $dte-error.txt`; `rm $dte-dump.log`; } else { print "You have errors. Please look at $dte-error.txt and $dte-dum +p.log.\n\n"; } #--------------------------------------------------------------------- +---------#
Thanks for any guidence even if it is off to the back of the class :) -Kevin

Replies are listed 'Best First'.
Re: Trapping errors in Net::Telnet::Cisco
by ig (Vicar) on Jun 07, 2011 at 06:15 UTC

    You could use the errmode method of Net::Telnet. The default error mode is "die", which you are experiencing. The alternative is "return", in which case Net::Telnet (or, in your case Net::Telnet::Cisco) will return and you will have to check for errors.

    Something like the following might work:

    if ( $session ) { $session->errmode('return'); ### Login to privileged exec mode ### $session->login( Name => $uid , Password => $pawd ); # $session->enable( $pawd );

    Another solution, particularly if Net::Telent::Cisco is dieing despite setting the error mode to 'return', is to use eval to catch that. Maybe something like:

    foreach $entry (@iplist) { chomp ($entry); eval { &SDM }; if($@) { # log the error open ( ERROR_LOG, ">>$dte-error.txt") or die "Could not open $dte-error.txt.\n"; print "$entry: failed with $@ !\n"; print ERROR_LOG "$entry: failed with $@ !\n"; close ERROR_LOG; } }
      Thanks! I see where I was setting the return in the wrong spot. I had it in the line with the ftp commands. moving it to the top of the loop solved the problem of the die rather than return. The eval made a much better way to collect the errors to log. Again thanks... -Kevin

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://908339]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 20:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found