2017-07-07 40 views
-1

我使用expect並且已經建立了ssh connection。我想查看ls -1 /etc/folder/的結果。「expect」的輸出變量

我試圖

send "files=$(ls -1 /etc/folder/)" 
put $files 

,但失敗了。

我該怎麼做?

回答

1

你得到的問題是變量在本地和遠程系統上的變量,它們根本不是同一件事。雖然是的,但是可以構建使系統間自動共享變量(似乎是)的系統,但默認情況下絕對不會發生這種情況。這是一個概念性的東西,你必須得到或者你不會寫出正確的代碼,除非意外。

除此之外,你有你不正確發送的是一個較小的問題的命令,而不是處理結果回來。假設您知道遠程系統上的提示只是一個$,並且文件永遠不會以此開始,您需要執行此操作。

# Note the \r; it is like pressing <Return> and is important! 
send "ls -1 /etc/folder \r" 
# Now we collect the output: 
set files {} 
expect { 
    "$ " { 
     puts $files 
    } 
    -re {^[^$].*$} { 
     # This grabs a line that doesn't start with $ and adds it to the list 
     lappend files $expect_out(0,string) 
     # Now we go back to waiting for another line or the prompt 
     exp_continue 
    } 
} 

而這正是如何做到這一點。是的,它確實需要相當多的代碼...

+0

你可能會發現你在文件列表中的每個元素的末尾回車。你可能想'-re {lappend文件$ expect_out(1串){^ \ R $([^ $] *)?}; exp_continue}' –