Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Cisco Telnet

by mvip (Acolyte)
on Aug 16, 2013 at 11:17 UTC ( #1049725=perlquestion: print w/ replies, xml ) Need Help??
mvip has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, Im using perl to connect on a cisco routers and make some changes ... the script works ok ... but i have some problem if some of the routers are off ... i have for example 100 router and it start from first one and goes one, when some way on a 20 tih router ( and he is off ) it stop. So how can i modify to continue if some of the routers are off and write something in the log file that that device is off .... im using Net::Telnet::Cisco and have this

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",); $session->login('admin', 'admin'); #some command $session->cmd ("show running-config "); $session->close;

and if some router is down the script stop, not continue. So my solution is to put "if" where with ping command check if the host is alive or not ... but i think you all can come with better solution tnx in advanced ...

Comment on Cisco Telnet
Download Code
Re: Cisco Telnet
by McA (Curate) 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

      Tnx for the reply ... McA i wast trying with that but not working ... i put 10 seconds for timeout but nothing the script just end stoped ....

        Hi,

        In Re: Cisco Telnet ~~David~~ is showing you how to wrap the call to login with an eval block to catch an exception. Then you are able to log that issue and proceed in your loop.

        Is that what you meant?

        McA

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;
    ~~David~~
      I have scripts that do the ping first test.

      I like Net::Ping::External as it allows, on linux, non-root users to run the scripts.

      -Scott

      Im new to perl im still reading the "Beginning of perl" book, and im not familiar with eval ... i was riding about it and i think i get the point for something... for example

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

      Im new to perl im still reading the "Beginning of perl" book, and im not familiar with eval ... i was riding about it and i think i get the point for something... for example

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

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.
Re: Cisco Telnet
by McA (Curate) 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

      Hi, tnx McA i get it now ... i have put the eval in wrong place. Here is what iv done

      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.

      i would like if its possible to write in the log file the error form eval in this code. From "perllogrouter/router$i.log"the eval error to be put there if its possible.

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

        sorry for the noob questions but i done this. So here it is for shearing

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1049725]
Approved by ww
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-09-16 20:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (47 votes), past polls