• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2195
  • Last Modified:

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>

0
matcor9925
Asked:
matcor9925
  • 5
  • 4
2 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now