Expect Script Order of Operations
expect, tcl
Solution
Expect actually processes `send` asynchronously, according to how fast the spawned application can actually accept the data (there are also some other rate limiters too). In order to get things to wait, you need to use `expect`.
The fix is to put the `puts` after the `expect eof` (or put an `expect` of something else beforehand, such as the prompt you get after doing the `delete flash:…`).
Problem
So first off I will admit to being very new to expect script I've been playing with it for about 3 days now. I picked up the O'Reilly book and am working my way through it. However I'm a bit baffled about expect's flow, and was hoping for some guidance. I have the following code snip below. The top half of the if statement works perfectly. However the elseif portion does not work as expected. It executes, just out of order. It first processes the puts line then runs down through the send commands. I am wondering why it does this and how to make it execute in order. Also another tweak I would like to make is that when there isn't a match for $MD5, I don't want to wait for the timeout. So basically I'd like to expect $MD5 or anything that is not $MD5 so that I don't have to sit around for the default timeout. I realize I could do expect -timeout 1 "$MD5" and shorten the window, but I was wondering if there was a more elegant way of handling this. Thank you in advance. ``` expect "$MD5" if {$MD5 == $expect_out(0,string)} { send "config t\r"; send "no boot system\r"; send "boot system flash:$IOS\r"; send "exit\r"; send "wr mem\r"; expect "OK"; send "exit\r"; puts -nonewline "\nIOS Upgrade Successful and Bootvar changed.\n" exit } elseif {$MD5 != $expect_out(0,string)} { send "delete flash:$IOS\r"; send "\r"; send "\r"; send "exit\r"; puts -nonewline "\nIOS Upgrade FAILED MD5 Hash did not match!\n"; expect eof exit } ```