在您期待之后有超时处理失败的连接。有些系统甚至可能不够友善,拒绝连接。以下是在不通知用户它的ssh产生超时的系统上测试的。
#!/usr/bin/env expect
# invoke example ./telnet.exp 127.0.0.1 username password
set IP [lindex $argv 0]
set User [lindex $argv 1]
set Password [lindex $argv 2]
# Set timeout from default 10 seconds
set timeout 5
# connect without asking yes/no for known_hosts
spawn ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null [email protected]$IP
# I'm not being cheeky, I left out the p/P because I avoid regex where possible
expect {
-nocase "refused" { spawn telnet $IP }
timeout { spawn telnet $IP }
"assword:" { send "$Password\r" }
}
interact
这是我到你的片段建议的变化(这是未经测试)
foreach line $MACHINES {
spawn ssh -q [email protected]$line
expect {
-ex "ssh: connect to host $line port 22: Connection refused" {
spawn telnet -l [email protected]$line; continue
}
timeout {
spawn telnet -l [email protected]$line; continue
}
}
}
用'希望-d'运行脚本,看看什么是错的。 – pynexj