Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Telnet Cisco routers

by IvanAK (Acolyte)
on Apr 11, 2013 at 19:52 UTC ( #1028221=perlquestion: print w/replies, xml ) Need Help??

IvanAK has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, im new in perl and i love it and trying to learn from the beginning, but the time is the worst enemy and i need help with one little program that i start and now im trying to improve it. So here is what i have for now:

use Net::Telnet::Cisco; for ($i = 1; $i <= 254; $i++){ $device = "10.10.$i.1"; my $session = Net::Telnet::Cisco->new(Host => "$device", Input_log => "perllogrouter/router$i.log", ); $session->login('admin', 'admin'); $session->cmd('conf t'); @output = $session->cmd ('some command'); $session->close; }

So my problem or improvement that i want to make is, I have some routers that have different password and on some they dont ask for username,just password. So how can i improve this ... any suggestions or examples ??? hhhmmm and also im not having all subnet active,/i mean, for example is not active, it doesn't exist, also doesn't exist etc. Tnx in advanced

Replies are listed 'Best First'.
Re: Telnet Cisco routers
by Illuminatus (Curate) on Apr 11, 2013 at 20:42 UTC
    If this is your first post, then thank you for using code tags :)

    Now, to your problem. Seems like you could use hashes or arrays to store the exceptions:

    my %users = {"1" =>"none", "2" => "admin"...}; my %pwds = {"1" =>"admin", "2" => "foobar"...};
    You can either define these in the script or read them in from a file. Then, in your loop, you can
    ... my $curpwd = "admin"; if (defined $pwds{$i}) { $curpwd = $pwds{$i}; } if ($users{$i} eq "none") { $session->login(Password => $curpwd); } else { $session->login(Name => 'admin', Password => $curpwd); }
    note that the login method for this module does not require a name, but if you want to skip it, you have to used named parameters.


      Tnx for the reply Illuminatus ;). Im new in perl like i sad So i have some funny question (for learning purpose), is there a exception in this "Net::Telnet::Cisco", like if log in attempt is incorrect then try one of this user and password? Tomorrow i will try the solution you gave me and see what will be. I will post the results ... of course to shear ;)and continue to read the "Beginning Perl - Curtis "Ovid" Poe" tnx again for the help ;)

      Hi i was trying the suggestion you gave me and i have some problems or nubs questions ... here is what i have

      use Net::Telnet::Cisco; for ($i = 4; $i <= 10; $i++){ $device = "172.16.$i.251"; my $pe = $i; my %users = ("6" =>"none", ); my %pwds = ("6" =>"thispass", ); my $curpwd = "admin"; if (defined $pwds{$i}) { $curpwd = $pwds{$i}; } if ($users{$i} eq "none") { $session->login(Password => $curpwd); } else { $session->login(Name => 'admin', Password => $curpwd, Input_log => " +perllogrouter/router$i.log",); } $session->cmd ("sh running-config | redirect +$pe.txt"); $session->close; }

      and this is the error i get when i try to run this


      Name "main::device" used only once: possible typo at line 5.

      Use of uninitialized value within %users in string eq at line 16.

      Can't call method "login" on an undefined value at line 19.


      Any suggestion about this because im still learning the perl and some of the errors that i get im not quite sure what are telling me

        You have violated the first commandment, "Thou shalt not write programs without 'use strict' and 'use warnings'". Your declaration of $device likely conflicts with a global in the telnet package.


Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1028221]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2023-09-23 03:40 GMT
Find Nodes?
    Voting Booth?

    No recent polls found