Version 10 of How to access the result of a remote command in Expect

Updated 2002-11-20 18:35:53

For convenience, I'll define the prompt and command first:

   set prompt "date"    ;# our remote command
   set prompt "% "      ;# our shell or whatever prompt we have

Now we can do useful things:

   expect "$prompt"     ;# wait for prompt
   send "date\r"        ;# send command
   expect "$prompt\r\n" ;# discard command echo

Finally, match and save the result and discard the next prompt:

   expect -re (.*)\r\n$prompt\r\n"

Now the result is in $expect_out(1,string).

If you want to read more about this, start on page 113 of BOOK Exploring Expect. This idiom is difficult for some programmers; the point is that Expect never gives direct access to "the result of a (foreign) command"; it simply transacts send-expect dialogues, and one has to know how to subtract out prompts, newlines, and other conventional decorations.

PS: If the result might be more than 2000 characters, you'll need to use the match_max command to increase the buffer space.


"Remote input-output with Expect" gives a model for line-oriented result access.


Category Expect