Re: Cisco Telnet
by McA (Priest) on Aug 16, 2013 at 11:37 UTC
|
Checkout whether Net::Telnet::Cisco has the possiblity to define a network timeout.
Update: A quick look at the manual pages let me find this:
$ok = $obj->login([Name => $username,]
[Password => $password,]
[Passcode => $passcode,] # for Secur-ID/XTACACS
[Prompt => $match,]
[Timeout => $secs,]);
I would try to find a feasible value for 'Timeout'.
McA | [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] [d/l] |
|
|
|
|
|
Re: Cisco Telnet
by ~~David~~ (Hermit) on Aug 16, 2013 at 18:46 UTC
|
You could always eval the login:
use Net::Telnet::Cisco;
for ($i = 1; $i <= 254; $i++){
$device = "172.16.$i.251";
my $session = Net::Telnet::Cisco->new(Host => "$device",
Input_log => "perllogrouter/router$i.log",);
eval { $session->login('admin', 'admin') or die "Could not connect
+to $device" };
if ( $@ ){ # log here #; next }
#some command
$session->cmd ("show running-config ");
$session->close;
| [reply] [d/l] |
|
|
| [reply] |
|
|
use Net::Telnet::Cisco;
for ($i = 1; $i <= 254; $i++){
$device = "172.16.$i.251";
my $session = Net::Telnet::Cisco->new(Host => "$device",
Input_log => "perllogrouter/router$i.log",);
eval { $session->login('admin', 'admin') or die "Could not connect
+to $device" };
in this part i understand that "for loop" make from 1 to 254 and ctr... the part of eval is execute this { $session->login('admin', 'admin') or die "Could not connect
+to $device" };
and i guess this means if it's negative or not connected if ( $@ ){ # log here #; next } go to the next part ... and the next part is again "for loop". So my question is how can i go the for loop again ... | [reply] [d/l] [select] |
|
|
use Net::Telnet::Cisco;
for ($i = 1; $i <= 254; $i++){
$device = "172.16.$i.251";
my $session = Net::Telnet::Cisco->new(Host => "$device",
Input_log => "perllogrouter/router$i.log",);
eval { $session->login('admin', 'admin') or die "Could not connect
+to $device" };
in this part i understand that "for loop" make from 1 to 254 and ctr... the part of eval is execute this
{ $session->login('admin', 'admin') or die "Could not connect
+to $device" };
and i guess this means if it's negative or not connected if ( $@ ){ # log here #; next } go to the next part ... and the next part is again "for loop". So my question is how can i go the for loop again ... | [reply] [d/l] [select] |
Re: Cisco Telnet
by McA (Priest) on Aug 22, 2013 at 07:11 UTC
|
Hi,
after doing all advice theoretically (I'm not a network admin), I coded a small test to see where the problem is:
1 #!/usr/bin/env perl
2 use 5.010;
3 use strict;
4 use utf8;
5 use warnings qw(all);
6
7 use local::lib 'cisco';
8
9 use Net::Telnet::Cisco;
10
11 my $session = Net::Telnet::Cisco->new(
12 Host => "192.168.168.2",
13 Timeout => 3,
14 );
15 $session->login(Password => 'secret');
A run of that shows:
problem connecting to "192.168.168.2", port 23: connect timed-out at a
+m304.pl line 11
AAAAHHH, the exception is thrown on line 11. It seems that the method new is doing a connect to host mentioned. IMHO that's an API misdesign, but anyways.
Conclusion:
- eval around the new method.
- reduce a problem to the minimal to find the problem.
BUT: Now the interesting part. Putting an eval around the method new can catch the exception, but not the exception message. And that's worth another thread herein.
Best regards
McA | [reply] [d/l] [select] |
|
|
eval { my $session = Net::Telnet::Cisco->new(Host => "$device",
Input_log => "perllogrouter/router$i.log",
);
$session->login(Password => $curpwd);
if ($session->enable("$curpwd"))
{
# Execute a command
@output = $session->cmd ('show running-config | section hostname')
+;
print "@output\n";
print " ======================================================\n";
}
$session->close;
}; if ($@) {print "Error: $@\n"}
before i was put the eval in "$seesion->login(password=>$curpwd);" , now its works fine ... tnx again for the help and support. | [reply] [d/l] |
|
|
eval { my $session = Net::Telnet::Cisco->new(Host => "$device",
Input_log => "perllogrouter/router$i.log",
);
$session->login(Password => $curpwd);
if ($session->enable("$curpwd"))
{
# Execute a command
@output = $session->cmd ('show running-config | section hostname')
+;
print "@output\n";
print " ======================================================\n";
}
$session->close;
}; if ($@) {print "Error: $@\n"}
| [reply] [d/l] |
|
|
if ($@) {
my $logtext = 'ruterproba.txt';
open my $fh, '>>',$logtext
or die "Nemoze da zapise vo $logtext poradi:$! ";
print $fh "Error: $@\n"}
I dont know why i start learning perl, but i have to admit i have finish great jobs with him :)) i love it and im continuing to learn more and more :) and tnx for support .....
| [reply] [d/l] |
Re: Cisco Telnet
by Anonymous Monk on Aug 17, 2013 at 02:07 UTC
|
Just change errmode() to "return". See the section "What To Know Before Using" in the Net::Telnet documentation.
| [reply] |
Re: Cisco Telnet
by Anonymous Monk on Apr 01, 2015 at 15:49 UTC
|
The Net::Telnet module by default dies when a timeout occurs.
You should add the 'Errmode' type in the constructor:
my $ip = 'x.x.x.x';
my $session = Net::Telnet::Cisco->new( Timeout => 10, Errmode => 'retu
+rn');
And next, a handle for the error. In this case, the script closes the tcp session:
$session->open($ip);
if($session->errmsg){
$session->close;
}
Check the Net::Telnet module for more info. I personally use this module to automate Cisco tasks because I have found some problems with the Net::Telnet::Cisco module. | [reply] [d/l] [select] |