我不知道发生了什么,但我没有从执行的远程命令获得完整的输出可能因为期望内部缓冲区被执行.
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" "$username@$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"
}
Run Code Online (Sandbox Code Playgroud)
这就是我执行它的方式:
./sendcommand aehj SWEs-elmPCI-B-01.tellus comnet1 "terminal length 0;show int description" "(.*#)$"
Run Code Online (Sandbox Code Playgroud)
我只有当我删除时才得到完整的输出,log user 0但是当我在上面的fucnction sendcommands中使用puts命令时,我得到了大约90%的结果,最后10%的尾随数据没有显示.
我想的一种方法是在期望中使用正则表达式的否定但它似乎不起作用.
expect {
-re ! $prompt
{puts $expect_outcome(buffer)}
}
Run Code Online (Sandbox Code Playgroud)
编辑:当执行约5或7次时,我获得所有输出一次
经过一番搜索后,我想出了这个,似乎可行,但请让我知道任何例外或更好的答案:
我设置 match_max = 1000 然后
expect {
-re $prompt
{send -- "$element\r"}
full_buffer {
append outcome $expect_out(buffer)
exp_continue
}
}
append outcome $expect_out(buffer)
puts $outcome
Run Code Online (Sandbox Code Playgroud)
但当我设置 match_max = 10000 或 100 时,它仍然再次失败