Expect + grep


I have written a GUI tool which issues commands on remote hosts using
Expect. The tool displays everything that is returned as a result of
“send …\r”. It works very well except when using ’grep’. I found in
c.l.t. some discussions on the issue of Expect-grep (mis)match, but I
am still unsatisfied…
The problem, as before, is that I don’t always get back all the data
which’ grep’ generates.

What I don’t understand is that Expect simulates an interactive CLI.
In such a case, when a host returns the prompt, the command (buffering
or not) finished running and by then displayed all of its output. But
according to previous postings on the subject, this is not always the
case when Expect is used. Also, depending on the amount of data
generated, only one of the following seems to work:

-re "(\\\$|.*bash.*)" {
# exp_send_user "\nPrompted\n"

-re "(\[^\r]*)\r\n" {
# display line:
send_user "\nLINE: <$expect_out(1,string)>\n"


full_buffer {
# send_user "FULL BUFFER\n"
append buffer $expect_out(buffer)

-re "(\\\$|.*bash.*)" {
# exp_send_user "\nPrompted\n"
(parse $buffer and display)

Is the Expect + grep a battle not worth fighting? I can use a
workaround, such as:
ssh host “grep xyz /a/b/c” > local_file
and display the file content but that would necessitate a separate
Expect script (I want to keep using the generic Expect script which
executes _any_ command remotely).