Expecting the Unexpected in Expect

a 夏天 提交于 2019-12-11 04:43:29

问题


How can I give an instruction to expect, when it sees anything other than what it expects?

Example, I attempt to automate a login. For anything other than a successful attempt, I need the script to mail me an error. How do I expect unexpected output? (I know some outcomes, like connection denied, or wrong password, but I want to catch everything)


回答1:


Try like this:

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
}


来源:https://stackoverflow.com/questions/44132153/expecting-the-unexpected-in-expect

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!