在 Expect 中期待意外

Expecting the Unexpected in Expect

当它看到任何与预期不同的东西时,我如何给出预期的指令?

例如,我尝试自动登录。对于除了成功尝试以外的任何事情,我都需要脚本向我发送错误消息。我如何期待意外的输出? (我知道一些结果,比如连接被拒绝,或者密码错误,但我想抓住一切)

这样试试:

set timeout 10; # set a reasonable timeout

# expect and send username/password ...

set success 0
set err_msg ""
expect {
  "Login success!" {
    set success 1
  }
  eof {
    set err_msg $expect_out(buffer)
  }
  timeout {
    expect *
    set err_msg $expect_out(buffer)
  }
}

if {! $success} {
  send_mail $err_msg
  exit 1
}