You need to set the prompt that Net::Telnet should look for. Otherwise it's waiting for something it'll never get.
See "perldoc Net::Telnet" or here: http://search.cpan.org/~jr
Main Topics
Browse All TopicsHi,
I am using the Net::Telnet module and calling $telnet->cmd($command) to invoke a process on a remote 2K machine, from a shell script on linux. The process is invoked fine but the 'cmd' command does not return even after the remote process is over. The script just waits and waits and then times out.
Any suggestions why this is happening or any debugging tips?
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
You need to set the prompt that Net::Telnet should look for. Otherwise it's waiting for something it'll never get.
See "perldoc Net::Telnet" or here: http://search.cpan.org/~jr
I am launching an exe application, which does some file writing and returns.
And yes I am issuing a $telnet->waitfor command after calling the cmd command. All the commands I issued prior to cmd, worked fine...they were issued with the $telnet->print(' ') format. I need to use cmd for this as the command to be issued has variables substituted at runtime. I am waiting for the correct prompt. It just does not return.
You must use $telnet->prompt() to set the correct prompt before using $telnet->cmd().
$telnet->cmd() is waiting for a prompt in order to return the output.
Your $telnet->waitfor() isn't even being reached until $telnet->cmd() times out.
Use $telnet->print() instead of $telnet->cmd() if you want to use $telnet->waitfor().
Adding to what Tim said
You can set the prompt using $telnet->prompt(), or when you create the object
$telnet = new Net::Telnet (Prompt => YOUR_PROMPT_HERE);
$telnet->cmd(<command>) will send <command> (followed by the output record seperator, normally "\n") and wait for the prompt (as you told it the prompt should look like). The $telnet->print(<command>) will not wait for the prompt.
What is your prompt set to in your telnet object, and what does the prompt actually look like?
Does new lines or spaces make a difference? Instead od D:\failover, I am now trying just D:\> and I am using /D:\\>$/ for the Prompt regex. I tried a space between the > and $ too...Nothing works...I only get command timed out error...
BTW with 4 backslashes for a single one in the Prompt regex, the program just exits without even running the remote command. Help please.
Maybe the login process didn't swallow the original prompt, so $telnet->cmd(...) sends the command, and reads the prompt and closes the connection before the command gets executed.
Try setting the prompt when you create your Net::Telnet object like Adam314 said...
$telnet = new Net::Telnet (
Prompt => '/[A-Z]:\\\\.*>\s*$/i'
);
My prompt was always in the telnet object create statement...Now I put the exact same prompt regex, in the telnet object create statement, as above and the same thing happens...with 2 backslashes, the command executes remotely but the perl script times out...and with 4 backslashes, the command does not execute, no timing out, the script just exits without doing anything...no error message is generated either.
#!/usr/bin/perl
use Net::Telnet;
open (fp,"vserver");
$vip = <fp>;
chomp($vip);
$telnet = new Net::Telnet ( Timeout=>500,
Errmode=>'return',
Prompt=>'/^[A-Z]:.*>\s*$/i
Dump_Log => 'dump.txt',
Input_log => 'input.txt');
$telnet->open('10.51.1.19'
$telnet->waitfor('/login: $/i');
$telnet->print('Administra
$telnet->waitfor('/passwor
$telnet->print('acopia');
$telnet->waitfor('/Documen
$telnet->print('D:');
$telnet->waitfor('/D/i');
$com="failover\\failoverti
$res=$telnet->cmd(String=>
$emsg=$telnet->errmsg;
print "\nErrMsg: $emsg";
The scripty times out at the $telnet->cmd line. I tried $telnet->cmd($com) too. Didn't work. The prompt from the remote machine is "D:\>".
So it was the waitfor prompt that was the culprit all this time??? and not the telnet->new prompt ???
Well...I saw 2 different behaviors when using 2 and 4 slashes in the prompt attribute of the telnet obj create statement. When I used 2, the remote command would execute but the perl script would time out. When I used 4 slashes, the remote command did not execute and the perl script just ran to the end of the script and exited. I can imagine the 'just eating the command and discarding' situation happenning in the second case...
but again, how would this be linked to the waitfor prompt??
>but again, how would this be linked to the waitfor prompt??
It wouldn't be.
Given what you have now told me, I would say that with the correct prompt (using four backslashes) matched something which $telnet->waitfor('/D/i') didn't remove from the buffer. So it sent the command and immediately read what the last waitfor didn't swallow, and disconnected, and Windows discarded any data waiting to be sent to the shell.
Business Accounts
Answer for Membership
by: geotigerPosted on 2005-12-22 at 11:02:29ID: 15536423
What command did you invoke on the remote machine?