2011-08-04 133 views
1

我遇到了一些麻煩發送命令,並從我設置的telnet連接接收文本/數據。在perl中使用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會話中。

+0

我不知道如果我得到你的問題,'$ telnetCon-> CMD( 「」);'超時?是'viewtask'等待輸入,任何機會? – Hasturkun

+0

嗯有點當我第一次進入viewtask ...它打印一些文件,然後等待另一個輸入來繼續顯示文件的其餘部分 – Shahab

回答

1

Net::Telnetcmd方法寫入您的命令(附帶一個\n)並等待提示。這與您的程序等待輸入以完成輸出不兼容。

我覺得你的情況,你可能需要使用的printgetlines和/或waitfor的組合來得到這個工作

+0

我試過但沒有運氣,與getlines它打印一切,直到,包括,命令 – Shahab

+0

@Shahab:如果我理解正確,你應該可以做類似'...-> print(「yourcmd」);''...-> waitfor(/ ^:/);''。 ..-> print(「」);'等等 – Hasturkun

+0

奇怪的是,它不匹配:在行首 – Shahab