|
|
Question : Net Telnet Cisco > script quits when host is non-responsive
|
|
Hello,
I'm using Net::Telnet::Cisco 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 would like the script to 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:
#!/usr/bin/perl #
use Net::Telnet::Cisco;
#Username for router print "Enter username\n"; $user = ; chomp($user);
#password for router print "Enter password\n"; $pass = ; chomp($pass);
#CSV file with router IP and commands print "Enter filename\n"; $State = ; chomp($State);
open (INDB, "./$State.csv") or die "cannot find file";
#reads every line in CSV file while () { $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;
}
Example of CSV file with hosts/commands:
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
|
Answer : Net Telnet Cisco > script quits when host is non-responsive
|
|
In the code you posted, line 36 is my $enable_password = "$pass"; which is not causing the problem. I'll assume that your line 36 is: my $session = Net::Telnet::Cisco->new(Host => $CISCO_IP, Input_log=> "$SiteNumber.txt", Timeout=> '120');
If this is the case, try this:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
|
...
my $enable_password = "$pass";
my $session;
eval {
$session = Net::Telnet::Cisco->new(Host => $CISCO_IP, Input_log=> "$SiteNumber.txt", Timeout=> '120');
}
if($@) {
warn "Could not connect to $CISCO_IP: $@\n";
next;
}
$session->login($username, $password);
...
|
Open in New Window
Select All
|
|
|
|
|