在perl中使用Telnet时出现问题?

时间:2011-08-04 14:41:03

标签: perl telnet

我在发送命令和从我设置的telnet连接接收文本/数据时遇到了一些麻烦。

 #!perl
 #Telnet.pl

use Net::Telnet;

# Create a new instance of Net::Telnet, 
my $telnetCon = new Net::Telnet (Timeout => 20,
                                Dump_Log   => "dump.log",
                                Input_Log => "infile.log",
                                Output_log => "output.log",
                                Prompt => '/\$ $/') or die "Could not make connection.";

# Connect to the host of the users choice                                
$telnetCon->open('');

#get username and password from user
my $CXusername = '';
my $CXpassword = '';

my $task = '50104'; # Get this input from the search

# Recreate the login
# Wait for the login: message and then enter the username
$telnetCon->waitfor(match => '/login:/i');

# this method adds a \n to the end of the username, it mimics hitting the enter key after entering your username
$telnetCon->print($CXusername);

$telnetCon->waitfor(match => '/Password:/i');

# does the same as the previous command but for the password
$telnetCon->print($CXpassword);

#Wait for the login successful message
$telnetCon->waitfor(match => '/$/');

$telnetCon->cmd("viewtask 50104");
$telnetCon->cmd(" ");
$telnetCon->cmd(" ");

@output = $telnetCon->cmd("who");
print @output;

($output) = $telnetCon->waitfor(match => '/$/');

print "Output: ",$output;

if($searched =~ /MODIFIED files in Task $_[1] :(.*?)The/s){
    # to Logout of the telnet connection
    $telnetCon->print("exit");

    #return the modified data 
    return $1;
}

告诉我,如果这个问题没有意义,我会试着改写它。 First View Second View

所以这是telnet视图。当我输入$ telnetCon-> cmd(“viewtask 50140”)时,我卡在第一张图像上。我想进入第二个图像并继续将命令输入我的telnet会话。

1 个答案:

答案 0 :(得分:1)

Net::Telnet cmd方法写入您的命令(附加\n)并等待提示。这与您的程序等待输入完成输出不兼容。

我认为在您的情况下,您希望使用printgetlines和/或waitfor的组合来实现此目的