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

In reply to Trapping errors in Net::Telnet::Cisco by kc6ovd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.