By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,661 Members | 1,330 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,661 IT Pros & Developers. It's quick & easy.

telnet to router

P: 14
Dear all,
I have written following script to loin to router bu it is showing error.

Expand|Select|Wrap|Line Numbers
  1. #!c:\Perl\bin;
  2. use strict;
  3. use warnings;
  4.  
  5.  my $hostname = 'REMOVED FOR YOUR PROTECTION';
  6.  my $password = 'REMOVED FOR YOUR PROTECTION';
  7.  
  8. use Net::Telnet();
  9.  my $telnet = new Net::Telnet ( Timeout=>40,Errmode=>'die');
  10. $telnet->open($hostname);
  11. $telnet->waitfor('/Password: $/i');
  12. print ("printing password");
  13. $telnet->print($password);
  14. $telnet->waitfor('/Router> $/i');
  15. print ("getting admin rights");
  16. $telnet->print('en');
  17. $telnet->waitfor('/Password: $/i');
  18. $telnet->print($password);
  19. print ("printing password");
  20. $telnet->waitfor('/Router#: $/i');
  21.  
Can't locate object method "new" via package "Net::Telnet" (perhaps you forgot t
o load "Net::Telnet"?) at h.pl line 9.


pls help me to rectify the problem
Sep 13 '09 #1
Share this Question
Share on Google+
17 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
@ravimath
Try removing the parenthesis after the module name, as such:

Expand|Select|Wrap|Line Numbers
  1. use Net::Telnet;
  2.  
They are not needed and are probably causing your problem.

Also, please do not include sensitive information in your posts (such as login information and IP Addresses). That is a security risk that you should always be wary of. I have removed them for you in your post.

Regards,

Jeff
Sep 13 '09 #2

P: 14
I have tried removing parenthesis, but still it gives same error.
thanks for your help
Sep 18 '09 #3

KevinADC
Expert 2.5K+
P: 4,059
Did you properly install the Net::Telnet module? Your code is right out of the modules example code so that shouldn't be the problem, so maybe the module installation wasn't done properly.
Sep 18 '09 #4

P: 14
can u tell me how to check whether i have installed install the Net::Telnet module properly or not ? rather tell me how to install install the Net::Telnet module.
Sep 18 '09 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
@ravimath
All the questions you have asked can be answered in the CPAN FAQ. It has a lot of good information. You can also find out how to figure out what modules are already installed.

Regards,

Jeff
Sep 18 '09 #6

P: 14
I have read cpan.faq , but it is not having sufficient info , regarding net::telnet installation on windows platform using ppm or any other method
can u tell me how to check whether i have installed install the Net::Telnet module properly or not ? rather tell me how to install install the Net::Telnet module.
pls help
Sep 21 '09 #7

P: 14
i have resolved following problemCan't locate object method "new" via package "Net::Telnet" (perhaps you forgot t
o load "Net::Telnet"?) at h.pl line 9., but now when i run that original script
it waits for getting input and after getting input it gives following error
'sfddg' is not recognized as an internal or external command,
operable program or batch file.
when i type something it shows nothing on screen , but it takes input.
even when i run another small script which includesonly one line
print $wkday;
it just returns to command prompt and does not show anything on screen

2)can u suggest some modifications, i want to achieve following
It should prompt for ip address of router , then ask for username , password and enable password and finally it should give output on screen of sh run of that router
Sep 21 '09 #8

numberwhun
Expert Mod 2.5K+
P: 3,503
@ravimath
For the modifications in #2, that should be quite simple for you to add. Why not try and post your code if you get stuck. Its just a simple matter of prompting the user and storing the answer(s).

Regards,

Jeff
Sep 21 '09 #9

P: 14
Expand|Select|Wrap|Line Numbers
  1. #!c:\Perl\bin; 
  2. use strict; 
  3. use warnings; 
  4.  
  5.  my $hostname = 'REMOVED FOR YOUR PROTECTION'; 
  6.  my $password = 'REMOVED FOR YOUR PROTECTION'; 
  7.  
  8. use Net::Telnet(); 
  9.  my $telnet = new Net::Telnet ( Timeout=>40,Errmode=>'die'); 
  10. $telnet->open($hostname); 
  11. $telnet->waitfor('/Password: $/i'); 
  12. print ("printing password"); 
  13. $telnet->print($password); 
  14. $telnet->waitfor('/Router> $/i'); 
  15. print ("getting admin rights"); 
  16. $telnet->print('en'); 
  17. $telnet->waitfor('/Password: $/i'); 
  18. $telnet->print($password); 
  19. print ("printing password"); 
  20. $telnet->waitfor('/Router#: $/i'); 
  21.  
I have already added this code pls suggest changes for my previous question
Sep 22 '09 #10

nithinpes
Expert 100+
P: 410
That should be simple.
Expand|Select|Wrap|Line Numbers
  1. print "\nEnter Router IP:";
  2. $hostname=<STDIN>;
  3. print "\nEnter password:";
  4. $password=<STDIN>;
  5.  
Sep 22 '09 #11

numberwhun
Expert Mod 2.5K+
P: 3,503
Ravimath, you need to PLEASE use code tags around your code that you put into the forums. It is not optional and is required. If you do not do it, we have to clean up behind you. If you notice your original post, you will see I did it there as well.

Regards,

Jeff
Sep 22 '09 #12

P: 14
what is code tags ? pls help regarding this . i am sorry b
coz i am new to this
Sep 24 '09 #13

Expert
P: 70
Code tags are used to distinguish plain text from Perl code snippets
in your posting. They help others to more clearly understand your question.
When you are typing your question into this forum, you should see
several icons above the box where you are typing. The icon that looks
like # will automatically add a pair of CODE tags for you.

It would also help others to more clearly understand your question
if you used full English words rather than abbreviations. For example,
"please" is clearer than "pls".
Sep 25 '09 #14

numberwhun
Expert Mod 2.5K+
P: 3,503
In addition, here is a link to the posting guidelines for asking a question, which fully explains code tags.

Regards,

Jeff
Sep 25 '09 #15

P: 14
I am attaching two small programs

1)
Expand|Select|Wrap|Line Numbers
  1. #!c:\Perl\bin\perl;
  2.  
  3. use warnings;
  4. my $hostname = 'x.x.x.x';
  5. use Net::Telnet;
  6. my $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die');
  7. print("connected");
  8. $telnet->open($hostname);
  9.  
  10. $telnet->print('username');
  11. $telnet->print('password');
  12. $telnet->print('show run ');
2)
Expand|Select|Wrap|Line Numbers
  1. #!c:\Perl\bin;
  2. use strict;
  3. use warnings;
  4. use Net::Telnet;
  5.  my $telnet = new Net::Telnet ( Timeout=>30,Errmode=>'die');
  6. $telnet->open('x.x.x.x');
  7. $telnet->login('Username', 'Password'); 
  8. print "ok";
  9. $telnet->cmd('who'); 
  10.  
  11.  timed-out waiting for command prompt at h7.pl line 7
for the first program it is not showing any error it just gives output connected
but it will not give any output of show run
for second program it is showing error as above,
please suggest changes
Sep 25 '09 #16

nithinpes
Expert 100+
P: 410
For the first program, the 'print' method only passes the command to telnet. It will not display the result on your console. If you want the result on console, use 'cmd' method to capture result. Alternately, you can use 'Output_log' and 'Input_log' options to save input & output of telnet session to a log file.

Expand|Select|Wrap|Line Numbers
  1. my $telnet = new Net::Telnet ( Timeout=>10,Errmode=>'die',Input_log => 'input.txt, Output_log=> 'output.txt'); 
  2. print("connected"); 
  3. $telnet->open($hostname); 
  4.  
  5. $telnet->print('username'); 
  6. $telnet->print('password'); 
  7. my @res= $telnet->cmd('show run '); 
  8. print "@res";
  9.  
For the second program, the problem may be that the command prompt that script is waiting for did not match with the one that it found(you can set the 'Prompt' option for 'cmd' and 'new' methods).
Else, you can use a combination of 'waitfor' and 'print' methods.

Expand|Select|Wrap|Line Numbers
  1. $telnet->waitfor('/login: /');  # put the prompt that you get(inside //)
  2. $telnet->print($username);
  3. $telnet->waitfor('/password:/');
  4. $telnet->print($pass);
  5.  
Sep 29 '09 #17

P: 14
Hi all,

I have written following script.

Expand|Select|Wrap|Line Numbers
  1. use Net::Telnet;use Term::ReadKey;@a=('x.x.x.x' ,'x.x.x.x);@b=('host1','host2' );$i=0; print "Enter username\n";$user = <STDIN>;chomp($user);ReadMode( "noecho", STDIN );print "Enter password\n";$pwd = <STDIN>;chomp($pwd);ReadMode ("original", STDIN) ; $epwd='password';while ($i<2){$telnet = new Net::Telnet ( Timeout=>10, Errmode=>'die');$telnet->open($a[$i]); $telnet->waitfor('/Username:/');$telnet->print($user);
  2. $telnet->waitfor('/Password:/');$telnet->print($pwd);$h=$b[$i].'>';$e=$b[$i].'#';open(MYOUTFILE, ">>PriStatus.txt");$telnet->waitfor('/'.$h.'/'); $telnet->print('en');$telnet->waitfor('/Password:/');$telnet->print($epwd);print $epw;$telnet->waitfor('/'.$e.'/');$telnet->print('sh int desc | inc PRI');@lines=$telnet->waitfor('/'.$e.'/');print MYOUTFILE @lines;print @lines;$telnet->print('sh run | inc dialer string');@lines=$telnet->waitfor('/'.$e.'/');print MYOUTFILE @lines;print @lines;$telnet->close;$i++; }  
In the above script i am logging in to each router and getiing PRI status and storing it in some text file , it works fine1)but how to telnet to router without using hostname 2)if first router is not reachable still it should move on to next router in the array , how to check whether it is reachable or not ?3)@lines vaiable stores only 255 bytes , if i want to store large data ( e.g show run ) how to achieve it.
Oct 16 '09 #18

Post your reply

Sign in to post your reply or Sign up for a free account.