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