Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using Telnet module, having trouble passing the prompt

Status
Not open for further replies.

whitshea

Programmer
Sep 16, 2001
7
0
0
US
I am just trying to get a simple script to work. Currently it is giving the following error:
"timed-out waiting for command prompt at ./test_telnet.pl line 11"
I don't know what I'm doing wrong... the prompt for this host is:
myuser@myhost{myfolder}>
Here's what I'm trying... Please help.

#! /usr/local/bin/perl -w

use Net::Telnet ();

$host="myhost";
$username="myname";
$password="mypassword";

$t = new Net::Telnet ( Timeout => 20, Prompt => '/myname\@myhost\{myfolder\}\> $/');
$t->open($host);
$t->login($username, $password);
@lines = $t->cmd("/usr/bin/who");
print @lines;
 
would there be embedded / characters in myfolder when the script is run? Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
No embedded / characters. Actually, I got the following version to work on one box after changing the prompt to />$/ and adding a "waitfor" after the login. I'm having a different problem on another box. The script is successfully logging into the box, and giving the prompt, but it doesn't issue the command, and the error returned says:
timed-out waiting for command prompt at ./test_telnet.pl line 11

line 11 is the login line. I'm really at a loss. Any thoughts?

#! /usr/local/bin/perl -w

use Net::Telnet ();

$host="myhost";
$username="myname";
$password="mypassword";

$t = new Net::Telnet ( Timeout => 20, Prompt => '/>$/');
$t->open($host);
$t->login($username, $password);
$t->waitfor('/>$/');
@lines = $t->cmd("ls");
print "@lines\n";
 
I think the original problem was that you were using single quotes and backslashes in your prompt string. As a result, the backslashes are becoming part of the prompt string.

By default, Net::Telnet will call 'die' on any error and leave the user wondering 'what happened?'. To find out more information you need to redefine the 'errmode' and enable logging. Here is your code with the error mode set to return and logging enabled. Hopefully running this code will clarify the problem. Good Luck.

Code:
#! /usr/local/bin/perl -w

use Net::Telnet ();

$host="myhost";
$username="myname";
$password="mypassword";
$promtp = '/>$/'; 
my $t = new Net::Telnet(
        Timeout     => '20',
        Prompt      => "/$prompt/",
        Dump_log    => 'dump.log',
        Input_log   => 'input.log',
        Output_log  => 'output.log',
);
$t->errmode('return');
$t->Net::Telnet::open($host);
$t->open($host);
$msg = $t->errmsg;
print "$msg \n" if ($msg);
$t->login($username, $password);
$msg = $t->errmsg;
print "$msg \n" if ($msg);
@lines = $t->cmd("/usr/bin/who");
$msg = $t->errmsg;
print "$msg \n" if ($msg);
print @lines;


 
Thanks for the assistance. I put in the error checking code, and it turns out that the command is being sent to myhost and output is being generated, according to the input.log file. But the output of the command is not being written to my variable:

@lines = $t->cmd("/usr/bin/who"); #@lines isn't being set
print @lines; #nothing is printing

Additionally, after "$t->login($username, $password);", I get the error, "timed-out waiting for command prompt".
After "@lines = $t->cmd("/usr/bin/who");", I get the error, "command timed-out".

I'm starting to wonder if the box I'm telnetting to is configured incorrectly or something. Has anyone had similar problems?

Whitney
 
In order to execute the $t->cmd, you need to successfully login. So if $t->login is timing out, any command you try will not return the expected output. The $t->login is probably failing because it isn't finding the prompt. Please post your script and the contents of input.log.
 
Amazingly, I changed the prompt to '/>/', and it worked. Thanks again for the help! Really appreciated!!

Whitney
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top