469,342 Members | 5,750 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

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

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
6 5223
nithinpes
410 Expert 256MB
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
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
410 Expert 256MB
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
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
3,503 Expert Mod 2GB
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
4,059 Expert 2GB
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.

Similar topics

31 posts views Thread by surfunbear | last post: by
2 posts views Thread by Paul Porcelli | last post: by
82 posts views Thread by Edward Elliott | last post: by
4 posts views Thread by Ignoramus6539 | last post: by
20 posts views Thread by Shawn Milo | last post: by
1 post views Thread by pchaw | last post: by
13 posts views Thread by Corey G. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.