Chapter 8. Handling A Process And A User

In this chapter, I will describe how to use the send and expect commands to interact with the user and a process in the same script. For scripts that require passwords, a common approach is to interact with the user only to get the password and then to automate the remainder of the program. I will describe how to do this in a secure manner and I will also describe other topics related to passwords and security.

It is desirable to suppress character echoing while prompting for passwords. I will describe how to do this along with a broader discussion of terminal modes and how you can control them to achieve a variety of other effects.

The send_user Command

In Chapter 3 (p. 71), send was used to print strings to the the standard output. The first program in that chapter printed out "hello world" and was just one command:

send "hello world\n"

But once a process has been spawned, the send command no longer prints to the standard output but instead sends strings to the spawned process. In the following script, the send command sends its argument to the spawned process, ftp:

spawn ftp
expect "Name"
send "anonymous\r"

The expect command works the same way. Initially, it reads from Expect’s standard input but as soon as a process has been spawned, expect reads from the process.

If the process dies and a new process is spawned, send and expect refer to the new process. In Chapter 10 (p. 229), I will describe how to use send and expect to communicate ...

Get Exploring Expect now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.