Connecting Tech Pros Worldwide Help | Site Map

telnet to router

Newbie
 
Join Date: Sep 2009
Posts: 14
#1: Sep 13 '09
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
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#2: Sep 13 '09

re: telnet to router


Quote:

Originally Posted by ravimath View Post

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

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
Newbie
 
Join Date: Sep 2009
Posts: 14
#3: Sep 18 '09

re: telnet to router


I have tried removing parenthesis, but still it gives same error.
thanks for your help
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Sep 18 '09

re: telnet to router


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.
Newbie
 
Join Date: Sep 2009
Posts: 14
#5: Sep 18 '09

re: telnet to router


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.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#6: Sep 18 '09

re: telnet to router


Quote:

Originally Posted by ravimath View Post

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.

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
Newbie
 
Join Date: Sep 2009
Posts: 14
#7: Sep 21 '09

re: telnet to router


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
Newbie
 
Join Date: Sep 2009
Posts: 14
#8: Sep 21 '09

re: telnet to router


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
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#9: Sep 21 '09

re: telnet to router


Quote:

Originally Posted by ravimath View Post

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

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
Newbie
 
Join Date: Sep 2009
Posts: 14
#10: Sep 22 '09

re: telnet to router


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
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#11: Sep 22 '09

re: telnet to router


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.  
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#12: Sep 22 '09

re: telnet to router


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
Newbie
 
Join Date: Sep 2009
Posts: 14
#13: Sep 24 '09

re: telnet to router


what is code tags ? pls help regarding this . i am sorry b
coz i am new to this
Newbie
 
Join Date: Sep 2009
Posts: 15
#14: Sep 25 '09

re: telnet to router


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".
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,567
#15: Sep 25 '09

re: telnet to router


In addition, here is a link to the posting guidelines for asking a question, which fully explains code tags.

Regards,

Jeff
Newbie
 
Join Date: Sep 2009
Posts: 14
#16: Sep 25 '09

re: telnet to router


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
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#17: Sep 29 '09

re: telnet to router


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.  
Newbie
 
Join Date: Sep 2009
Posts: 14
#18: Oct 16 '09

re: telnet to router


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