6
我不知道發生了什麼,但我沒有得到執行遠程命令的完整輸出,可能是因爲期望內部緩衝區正在被刪除。當expect_out(buffer)大小的內部緩衝區大小超過時,如何獲得期望的完整輸出?
proc SendCommands { Commands } {
global prompt log errlog
foreach element [split $Commands ";"] {
expect {
-re $prompt
{send -- "$element\r"}
}
set outcome "$expect_out(buffer)"
foreach line [split $outcome \n] {
foreach word [regexp -all -inline {\S+} $line] {
if {([string index [string trimleft $line " "] 0 ] == "%")} {
puts "$outcome"
exit 1
}
}
}
puts "$outcome"
}
}
set timeout 30
foreach host [ split $hosts "\;" ] {
spawn ssh -o "StrictHostKeyChecking no" "[email protected]$host"
match_max 10000000
expect {
timeout { send_user "\nFailed to get password prompt\n"; exit 1 }
eof { send_user "\nSSH failure for $host\n"; exit 1 }
"*?assword:*"
{
send -- "$password\r"
}
}
expect {
timeout { send_user "\nLogin incorrect\n"; exit 1 }
eof { send_user "\nSSH failure for $host\n"; exit 1 }
-re "$prompt"
{ send -- "\r" }
}
set timeout 300
SendCommands "$Commands"
}
我這是如何執行它:
./sendcommand aehj SWEs-elmPCI-B-01.tellus comnet1 "terminal length 0;show int description" "(.*#)$"
我得到完整的輸出只有當我刪除log user 0
但是當我使用puts命令在fucnction sendcommands上面我得到約90%沒有顯示末尾數據的10% 。
我想的一個方法是使用否定正則表達式的期望,但它似乎沒有工作。
expect {
-re ! $prompt
{puts $expect_outcome(buffer)}
}
編輯:我得到一次時,其執行約5或7倍