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

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

P: 2
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.
Aug 20 '08 #1
Share this Question
Share on Google+
6 Replies


nithinpes
Expert 100+
P: 410
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.  
Aug 21 '08 #2

P: 2
Hi nithinpes

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

nithinpes
Expert 100+
P: 410
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.
Aug 22 '08 #4

P: 1
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.  
Sep 9 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Sep 9 '08 #6

KevinADC
Expert 2.5K+
P: 4,059
The code as posted looks like it should not even compile because $login has never been declared properly.
Sep 9 '08 #7

Post your reply

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