471,892 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,892 software developers and data experts.

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


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;
  6. my $telnet = new Net::Telnet (Timeout=>10);
  7. $telnet->open($ip_addr)or print "can't open the server";
  9. $telnet->waitfor('/:/');
  11. $telnet->print($login);
  12. $telnet->waitfor('/:/');
  14. $telnet->print($password);
  15. $telnet->waitfor('/:/');
  17. $error=$telnet->errmsg;
  20. $telnet->cmd("mkdir ABC");
  21. $error=$telnet->errmsg;
  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 5309
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,);
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
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;
  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;
Sep 9 '08 #5
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:  $!";

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.


Sep 9 '08 #6
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
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.