Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Telnet Cisco routers

by Illuminatus (Curate)
on Apr 11, 2013 at 20:42 UTC ( #1028232=note: print w/ replies, xml ) Need Help??

in reply to Telnet Cisco routers

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.


Comment on Re: Telnet Cisco routers
Select or Download Code
Replies are listed 'Best First'.
Re^2: Telnet Cisco routers
by IvanAK (Acolyte) on Apr 14, 2013 at 15:13 UTC

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

Re^2: Telnet Cisco routers
by IvanAK (Acolyte) on Apr 16, 2013 at 20:48 UTC

    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.


        UFF :S sorry for that my mistake. Any way i use it but as noob i cant figuring still what im doing wrong ... for now im not in the work ( medical issue ) and i cant test it ... im trying now at home with some improvisation and here is what i got using this code

        use Net::Telnet::Cisco; use strict; use warnings; use diagnostics; my %users = ("6" =>"none", ); my %pwds = ("6" =>"thispass", ); my $i = 4; for ($i; $i <= 10; $i++){ my $device = "10.10.$i.1"; my $pe = $i; my $curpwd = "admin"; if (defined $pwds{$i}) { $curpwd = $pwds{$i}; } if ($users{$i} eq "none") { my $session->login(Password => $curpwd); } else { my $session->login(Name => 'admin', Password => $curpwd, Input_log + => "perllogrouter/router$i.log",); } $session->cmd ("sh running-config | redirect +oba$pe.txt"); $session->close; }
        and here is what i get from that ..
        C:\Users\mydesktop\Desktop\Perl>perl Global symbol "$session" requires explicit package name at +line 24. Global symbol "$session" requires explicit package name at +line 25. Execution of aborted due to compilation errors.
        After i put "my" in session at line 24 and 25 i get this
        C:\Users\mydesktop\Desktop\Perl>perl "my" variable $session masks earlier declaration in same scope at teln line 25. Useless use of private variable in void context at line 26. Use of uninitialized value within %users in string eq at li +ne 19. Can't call method "login" on an undefined value at line 22. C:\Users\mydesktop\Desktop\Perl>
        So pls can you suggest what im doing wrong ... im now im noob and this is easy but the time is not on my side this few weeks. Im trying to read this book but im still at Strict,Warnings,and Diagnostics Chapter3 Variables :) and yes we all understand variables but i just fall in love with perl and i dont like to skip any chapter or something :) love is come again :) .... tnx in advanced for the support and any help you gave me .....

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1028232]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2015-11-30 22:13 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (786 votes), past polls