matcor9925
Programmer
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
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