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:

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

matcor9925Asked:
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.

Adam314Commented:
When the script stops - does it hang?  Does it end?  Do you get any error messages?
0
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.
0
Adam314Commented:
>>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
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

matcor9925Author Commented:
Adam314,

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

problem connecting to "10.1.1.1", port 23: Unknown error at the\directory\with\the\script\login_test.pl 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 (10.2.2.2).

Thanks
0
Adam314Commented:
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";
    next;
}
 
 
$session->login($username, $password);
...

Open in new window

0

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\login_test.pl line 40, near ") {"
syntax error at the\directory\with\the\script\login_test.pl line 68, near "}"
Execution of the\directory\with\the\script\login_test.pl aborted due to compilation errors.
0
Adam314Commented:
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

0
matcor9925Author Commented:
Adam,

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.

Thanks,
0
matcor9925Author Commented:
Nevermind on the previous post. Thank you for your help!
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.