Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error handling telnetting to hosts in CSV file

Status
Not open for further replies.

matcor9925

Programmer
Nov 10, 2008
4
US
Hello,

I'm using the Net::Telnet::Cisco module to update configs on multiple Cisco routers (all are 1841 model). The script reads from a CSV file that has the router IP, and commands to be entered. The script runs fine unless a router does not respond to telnet in which case the script does not continue to execute. I need some help implementing error handling so the script will skip an unresponsive host and write the unresponsive host to a file, then continue to the next host in the CSV file. Also, after a reload command is entered for a particular router, the script does not continue to execute. Any help would be greatly appreciated.

Example of my perl script:

<SNIP>
#!/usr/bin/perl
#

use Net::Telnet::Cisco;

#Username for router
print "Enter username\n";
$user = <STDIN>;
chomp($user);

#password for router
print "Enter password\n";
$pass = <STDIN>;
chomp($pass);

#CSV file with router IP and commands
print "Enter filename\n";
$State = <STDIN>;
chomp($State);

open (INDB, "./$State.csv") or
die "cannot find file";

#reads every line in CSV file
while (<INDB>)
{
$SiteData = $_;
chomp ($SiteData);
($HostNumber, $loopback, $copytftpst, $tftpserver, $configpath, $deststcfg, $reload) =

split (/,/, $SiteData);

my $CISCO_IP = "$loopback";
my $username = "$user";
my $password = "$pass";
my $enable_password = "$pass";

my $session = Net::Telnet::Cisco->new(Host => $CISCO_IP, Input_log=> "$SiteNumber.txt", Timeout=> '120');

$session->login($username, $password);

my @output = "";
my @host = "";

# Enable mode
if ($session->enable($enable_password) )
{
@output = $session->cmd('show privilege');
@host = $session->cmd('sho run | include hostname');
print "@output for @host $loopback\n";
}
else
{
warn "Can't enable: " . $session->errmsg;
}

$session->cmd("$copytftpst\n$tftpserver\n$configpath\n$deststcfg\n$reload");
$session->close;

}
<SNIP>

Example of CSV file with hosts/commands:

<SNIP>
rtr1, 10.1.1.1, copy tftp startup-config, 10.0.0.1, rtr1/rtr1.1800.txt, startup-config, reload
rtr2, 10.2.2.2, copy tftp startup-config, 10.0.0.1, rtr2/rtr2.1800.txt, startup-config, reload
<SNIP>

Thank you,

Matt
 
Try alarms.

Code:
# most of your code

# a lexical block
eval {
   # set the alarm
   $SIG{ALRM} = sub { die; }
   alarm(30);

   # do something that is potentially time-consuming
   sleep(int(rand(100)));

   # if it passed, unset the alarm and continue
   alarm(0);
   print "the sleep survived!\n";
};
if ($@) {
   # $@ probably equals "Alarm clock."
   print "the alarm clock went off!\n";
}

you can set $SIG{ALRM} to whatever you want. The "die" there doesn't kill your entire script because the code was wrapped inside an eval, so it only kills the eval and sets $@ to be the text of the error (you could have the die say something else and $@ would equal that text).

I haven't tested alarms on Windows systems. If it doesn't work you can use threads.

Code:
use threads;
use threads::shared;

my $success : shared;
$success = 0;

my $thr = threads->create (sub {
   # set up a thread kill handler
   $SIG{KILL} = sub { threads->exit(); };
   # do your time-consuming stuff here

   # set $success = 1 when the thread looks
   # like it'll be okay
   $success = 1;
});

# execution immediately continues here
# at the same time the thread is executing
sleep 30;
if ($success == 0) {
   # the thread isn't survivng
   $thr->kill ('KILL')->detach();
}

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top