2017-08-06 48 views
1
#!/usr/bin/perl 
use Net::SSH::Expect; 
use warnings; 
use strict; 

#my($stdout, $stderr, $exit) = $ssh->cmd("ls -l /home/$usr") 
# Making an ssh connection with user-password authentication 
# 1) construct the object 
my $ssh = Net::SSH::Expect->new (
    host => "host", 
    password=> 'pwd', 
    user => 'user', 
    raw_pty => 1 
    #Expect=>log_file("finally.txt") 
    ); 


# 2) logon to the SSH server using those credentials. 
# test the login output to make sure we had success 
my $login_output = $ssh->login(); 
    if ($login_output !~ /Welcome/) { 
    die "Login has failed. Login output was $login_output"; 
    } 



# disable terminal translations and echo on the SSH server 
# executing on the server the stty command: 
$ssh->exec("stty raw -echo"); 




my $stdout = $ssh->send(chr(13)); 
my $stdout2 = $ssh->send("SDT-FI"); 
my $stdout3 = $ssh->send("ENG"); 
my $stdout4 = $ssh->send('SORT FI-WIP "84144"'); 
my $stdout5 = $ssh->send(chr(13)); 
my $stdout6 = $ssh->send("OFF"); 
my $stdout7 = $ssh->send(chr(13)); 

print($stdout3); 

#$expect->log_file("adp-n.txt"); 

#y $line; 
# returns the next line, removing it from the input stream: 
# while (defined ($line = $ssh->read_all())) { 
# print $line . "\n"; 

#} 

,所以我試圖打印$ stdout3這樣我就可以得到關於輸出 信息,但我一直「在connnn3.pl線50未初始化值$ stdout3在打印」越來越使用未初始化的值。我該如何解決這個錯誤?

是有什麼在我的代碼錯了? 我該如何解決這個問題?

更新,已解決! 爲什麼它返回「未初始化值的」的原因是因爲該功能

send() 

是無效的,所以不是我用

exec() 

這解決它

+0

當你有'print($ stdout2)'(和其他數字)時會發生什麼?祝你好運。 – shellter

+0

@shellter它給了我同樣的錯誤 – popeonhabbo123

+2

請不要編輯評論像_solved_到你的問題。相反,用適當的解決方案自己回答你的問題,以便其他人可以受益。堆棧溢出不是論壇,而是Q和A站點。 – simbabque

回答

4

the documentation of Net::SSH::Expect

void send($ string) - 將$ string發送到SSH服務器,不返回任何內容

因此,send顯然沒有返回值(無效),這就是爲什麼你要打印的send的(不存在)的返回值時,此警告。如果您想從服務器獲取數據,請使用peek,eatread_all或與文檔相似的類似文件。

+0

你能舉個例子嗎? – popeonhabbo123

+3

@ popeonhabbo123:你看過文檔了嗎?它包含示例。 –

+0

我解決了它,謝謝:)我用exec而不是它的工作。謝謝你讓我知道發送是無效的:)它真的幫助 – popeonhabbo123

相關問題