Tcl 为什么这个for循环在超时后没有中断?
如果发送的ssh命令超时,我需要它移动到列表中的下一个地址 它到达了我发送pw的地方,我需要它来打破它,如果它没有 当选。它只是挂着。为什么?Tcl 为什么这个for循环在超时后没有中断?,tcl,expect,Tcl,Expect,如果发送的ssh命令超时,我需要它移动到列表中的下一个地址 它到达了我发送pw的地方,我需要它来打破它,如果它没有 当选。它只是挂着。为什么? foreach address $perAddress { set timeout 10 send "ssh $address user someone\r" expect "word:" send "Stuff\r" expect { "
foreach address $perAddress {
set timeout 10
send "ssh $address user someone\r"
expect "word:"
send "Stuff\r"
expect {
"prompt*#" {continue}
timeout {break}
}
set perLine [split $fileData "\n"]
set timeout 600
foreach line $perLine {
send "$line\r"
expect "# "
}
send "exit\r"
expect "> "
}
expect
命令接受break
和continue
条件(因为它认为自己在内部是一个循环)。这意味着您需要执行以下操作:
set timedOut 0
expect {
"prompt*#" {
# Do nothing here
}
timeout {
set timedOut 1
}
}
if {$timedOut} break
但是,可能更容易重构代码,使与特定地址的整个交互都在一个过程中,然后使用return
:
proc talkToHost {address} {
global fileData
set timeout 10
send "ssh $address user someone\r"
expect "word:"
send "Stuff\r"
expect {
"prompt*#" {continue}
timeout {return}
}
set perLine [split $fileData "\n"]
set timeout 600
foreach line $perLine {
send "$line\r"
expect "# "
}
send "exit\r"
expect "> "
}
foreach address $perAddress {
talkToHost $address
}
我发现更容易专注于如何让一台主机正常工作,而不是让它们在整个主机负载中工作。(例如,当出现超时时,在进入下一个连接之前不清理连接;这会泄漏虚拟终端,直到整个脚本退出。)谢谢Donal。我将中断改为继续,它将转到perAddress中的下一个地址。