Expecting the Unexpected in Expect

2019-07-24 08:51发布

问题:

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
}


标签: expect