The Value Of spawn_id Affects Many Commands

The chess and write scripts are good examples of how spawn_id affects both the send and expect commands. To recap, send and expect communicate with the currently spawned process—that is, the process whose spawn id is stored in the variable spawn_id. Other commands that are affected by spawn_id include interact, close, wait, match_max, parity, and remove_nulls. In later chapters, I will describe still more commands that are affected by spawn_id.

As an example, here is a code fragment to close and wait on a list of spawn ids.

foreach spawn_id $spawn_ids {
    close
    wait
}

This loop could have been added to the earlier write script—except that the script effectively does the close and wait upon exit anyway. However, remember from Chapter 4 (p. 103) that programs that run in raw mode (such as telnet) often need explicit code to force them to exit. That code might be appropriate in such a loop.

Imagine writing a script that telnets to several hosts and simultaneously sends the same keystrokes to each of them. This script could be used, for example, to reboot a set of machines, change passwords, test functionality, or any number of things that have to be performed directly on each machine.

Get Exploring Expect now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.