Connecting Tech Pros Worldwide Forums | Help | Site Map

Perl code- using the Telnet module for remote login to a windows XP machine

Newbie
 
Join Date: Aug 2008
Posts: 2
#1: Aug 20 '08
Hi

I have written a perl script using the Telnet module. I need to remotely login in from one windows XP machine to another XP machine.

But i get errors which i can't figure out the reason.
Please someone help me its very very urgent.

here is my code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Net::Telnet;
  4.  
  5.  
  6. my $telnet = new Net::Telnet (Timeout=>10);
  7. $telnet->open($ip_addr)or print "can't open the server";
  8.  
  9. $telnet->waitfor('/:/');
  10.  
  11. $telnet->print($login);
  12. $telnet->waitfor('/:/');
  13.  
  14. $telnet->print($password);
  15. $telnet->waitfor('/:/');
  16.  
  17. $error=$telnet->errmsg;
  18.  
  19.  
  20. $telnet->cmd("mkdir ABC");
  21. $error=$telnet->errmsg;
  22.  
  23. $telnet->close;
In this code i trying to login to a remote machine using telnet. and the task is to create a directory on the telnet server.(dir ABC).

i get an error:
command timed-out at D:\Perl_RemoteLogin\RemoteLogin_Telnet.pl line 27

Please can any one tell me whats going wrong in the code.

nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#2: Aug 21 '08

re: Perl code- using the Telnet module for remote login to a windows XP machine


Try giving a timeout value for the commands that you pass.
Expand|Select|Wrap|Line Numbers
  1. $cmd="mkdir ABC";
  2. $telnet->cmd( String => $cmd, Timeout =>20,);
  3.  
Newbie
 
Join Date: Aug 2008
Posts: 2
#3: Aug 21 '08

re: Perl code- using the Telnet module for remote login to a windows XP machine


Hi nithinpes

This too didn't work. can you tell me where am i going wrong. i guess the matching is going wrong
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#4: Aug 22 '08

re: Perl code- using the Telnet module for remote login to a windows XP machine


Quote:

Originally Posted by sherrygomindes

Hi nithinpes

This too didn't work. can you tell me where am i going wrong. i guess the matching is going wrong

I am not sure what is wrong then..May be someone else would know.
Newbie
 
Join Date: Dec 2007
Posts: 1
#5: Sep 9 '08

re: Perl code- using the Telnet module for remote login to a windows XP machine


You'll probably find its failing on the command promt..its waiting for a certain prompt and not getting it. I've changed it so its just looking for : see if that works. What is your promt?
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.       use strict;
  3.       use Net::Telnet;
  4.  
  5.       my $telnet = new Net::Telnet (Timeout=>10);
  6.       $telnet->open($ip_addr)or print "can't open the server";
  7.       $telnet->waitfor(': $/i');
  8.       $telnet->print($login);
  9.       $telnet->waitfor(': $/i');
  10.       $telnet->print($password);
  11.       $telnet->waitfor(': $/i');
  12.       $telnet->cmd("mkdir ABC");
  13.       $telnet->close;
  14.  
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,572
#6: Sep 9 '08

re: Perl code- using the Telnet module for remote login to a windows XP machine


First, you really need to use code tags around the code you post to the forums. I have added them to your post so edit it and see how they are used.

As for your code, you don't want to do a print after the "open", you want to do a die and print the error that was produced, like so:

Expand|Select|Wrap|Line Numbers
  1. $telnet->open($ip_addr) or die "Can't open the connection:  $!";
  2.  

The $! will contain the error message from the connection attempt and give you a clue as to why it is failing. Always use die when performing an action like opening a file or a connection or such and print the error. That way, your debugging is a little easier.

Regards,

Jeff
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#7: Sep 9 '08

re: Perl code- using the Telnet module for remote login to a windows XP machine


The code as posted looks like it should not even compile because $login has never been declared properly.
Reply