Don Libes's suggestion for your scenario is as follows,
Sometimes it is even useful to say:
expect *
Here the * matches anything. This is like saying, "I don't care what's
in the input buffer. Throw it away." This pattern always matches, even
if nothing is there. Remember that * matches anything, and the empty
string is anything! As a corollary of this behavior, this command
always returns immediately. It never waits for new data to arrive. It
does not have to since it matches everything.
Reference : Exploring Expect
In this case, after your required match, better try to save the match to some variable then simply add the code expect *
at the last. This will empty the buffer. Your code can altered as below.
proc do_cmd {cmd id} {
set spawn_id $id
send "$cmd
"
#Looks like you are looking for a particular command to arrive
expect "$cmd
"
#Then you have one more expect here which is you want to get it
expect {
#Saving the value sub match to the variable 'result'
-re "
(.*)
" {set result $expect_out(1,string)}}
}
#Causing the buffer to clear and it will return quickly
expect *
return $result
}
Apart from this, there is one more way can be unsetting the expect_out(buffer)
content itself which will remove the 'buffer' index from expect_out
array which can be depicted as
unset expect_out(buffer)
When the next match happens, expect_out
array will be updated the index 'buffer' and we can have the fresh expect_out(buffer)
value. Replace the expect *
with the above code if you prefer to use this way.
This is quite a workaround kind of stuff to get what we want actually. You can go ahead with any approach. Choice is yours. :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…