Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Expect to monitor multiple processes

15 views
Skip to first unread message

Why Tea

unread,
Mar 6, 2007, 5:07:19 AM3/6/07
to
What is the proper way to use Expect to monitor multiple processes?

Example:
spawn telnet to host1
... send a commands to set up the system ...
spawn telnet to host2
... send a few commands to cause host2 talk to host1 ...
Now, return to host1 to check for outputs
... might need to go back and forth for many test scenarios ...

/Why Tea

Cameron Laird

unread,
Mar 6, 2007, 8:49:34 AM3/6/07
to
In article <1173175639....@h3g2000cwc.googlegroups.com>,

The Book has a chapter on this. Briefly, Expect has the intelligence
to manage handles to several spawned processes. I don't know what
kind of answer you're after apart from the pertinent chapter in the
book; it exactly addresses, "the proper way ..."

Is there a more specific question you have in mind? Are you after
example code?

Glenn Jackman

unread,
Mar 6, 2007, 12:24:18 PM3/6/07
to
At 2007-03-06 05:07AM, "Why Tea" wrote:
> What is the proper way to use Expect to monitor multiple processes?
>
> Example:
> spawn telnet to host1

set host1_id $spawn_id

> ... send a commands to set up the system ...
> spawn telnet to host2

set host2_id $spawn_id

> ... send a few commands to cause host2 talk to host1 ...
> Now, return to host1 to check for outputs
> ... might need to go back and forth for many test scenarios ...

expect -i $host1_id {something to see on host1}
send -i $host1_id "a command for host1\r"

expect -i $host2_id {something to see on host2}
send -i $host2_id "a command for host2\r"

even wait for both hosts simultaneously

expect {
-i $host1_id -re {something from host1} {
# do something...
}
-i $host2_id -re {something from host2} {
# do something...
}
}

--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry

Bruce Hartweg

unread,
Mar 6, 2007, 3:29:19 PM3/6/07
to
Glenn Jackman wrote:
> At 2007-03-06 05:07AM, "Why Tea" wrote:
>> What is the proper way to use Expect to monitor multiple processes?
>>
>> Example:
>> spawn telnet to host1
>
> set host1_id $spawn_id
>
>> ... send a commands to set up the system ...
>> spawn telnet to host2
>
> set host2_id $spawn_id
>
>> ... send a few commands to cause host2 talk to host1 ...
>> Now, return to host1 to check for outputs
>> ... might need to go back and forth for many test scenarios ...
>
> expect -i $host1_id {something to see on host1}
> send -i $host1_id "a command for host1\r"
>
> expect -i $host2_id {something to see on host2}
> send -i $host2_id "a command for host2\r"
>

note that the above method order the interaction, nothing
is going on with host2 until host1 is dealt with.

> even wait for both hosts simultaneously
>
> expect {
> -i $host1_id -re {something from host1} {
> # do something...
> }
> -i $host2_id -re {something from host2} {
> # do something...
> }
> }
>
>
>

this allows whoever is ready first to proceed.

And you can even look for the same thing from multiple
spawn_ids all together and in the processing you can
get which ID matched.

set spawn_list [list $host1_id $host2_id]

expect {
-i spawn_list
-re {pattern1} {
puts " matched pattern1 on spawnid: $expect_out(spawn_id)"
send -i $expect_out "next command"
}
{pattern2} {
# take some other action
}
}


Bruce

Uwe Klein

unread,
Mar 6, 2007, 3:44:26 PM3/6/07
to

[expect_background] is quite usefull for this.

uwe

0 new messages