Net Telnet Cisco > script quits when host is non-responsive


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:


use Net::Telnet::Cisco;

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

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

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

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";
     warn "Can't enable: " . $session->errmsg;



Example of CSV file with hosts/commands:

rtr1,, copy tftp startup-config,, rtr1/rtr1.1800.txt, startup-config, reload
rtr2,, copy tftp startup-config,, rtr2/rtr2.1800.txt, startup-config, reload

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When the script stops - does it hang?  Does it end?  Do you get any error messages?
matcor9925Author Commented:
The script stops completely and closes when it tries to telnet to a router with a broken network connection or after executing the reload command. If I remove the reload command and if all of the routers in the CSV file have network connectivity, the script works fine and will execute all commands on all routers listed in the CSV. I do not see any errors from the script because the window closes too quickly.
>>the window closes too quickly
I'm guessing this means you start the script by clicking an icon.  Try starting a prompt, and running the script from the prompt.  This way, if there are any errors, you'll see them.

On windows, a prompt is called "Command Prompt", and is usually in start->programs->accessories, or can be started through start->run -> type cmd.exe -> ok
Once at a prompt, change to the directory with "cd \the\directory\with\the\script" (no double-quotes)
Then start the script by typing it's name
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

matcor9925Author Commented:

The error I get (this is with the network connection broken to the router ip

problem connecting to "", port 23: Unknown error at the\directory\with\the\script\ line 36

The script will not attempt a telnet session with the next router in the CSV file at this point. I want it to skip the unresponsive host and go to the next router in the CSV (

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:

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";
$session->login($username, $password);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
matcor9925Author Commented:
Now I get the following errors:

syntax error at the\directory\with\the\script\ line 40, near ") {"
syntax error at the\directory\with\the\script\ line 68, near "}"
Execution of the\directory\with\the\script\ aborted due to compilation errors.
Line 7 in what I posted is missing a semi-colon:
  eval {
      $session = Net::Telnet::Cisco->new(Host => $CISCO_IP, Input_log=> "$SiteNumber.txt", Timeout=> '120');
  };   #NOTE THE ; HERE

matcor9925Author Commented:

The script now skips an unresponsive host beautifully. Can you help with writing the unresponsive host to a file? You've been a tremendous help so far.

matcor9925Author Commented:
Nevermind on the previous post. Thank you for your help!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.