It is surprising how little scripting is necessary to produce something useful. Below is a script that dials a phone. It is used to reverse the charges so that long-distance phone calls are charged to the computer. It is invoked with the phone number as its argument.
spawn tip modem expect "connected" send "ATD$argv\r" # modem takes a while to connect set timeout 60 expect "CONNECT"
The first line runs the tip
program so that the output of a modem can be read by expect
and its input written by send
. Once tip
says it is connected, the modem is told to dial using the command ATD
followed by the phone number. The phone number is retrieved from argv
, which is a variable predefined to contain the original argument with which the script was called.
The fourth line is just a comment noting that the variable being set in the next line controls how long expect
will wait before giving up. At this point, the script waits for the call to complete. No matter what happens, expect
terminates. If the call succeeds, the system detects that a user is connected and prompts with "login:
“.
Actual scripts do more error checking, of course. For example, the script could retry if the call fails. But the point here is that it does not take much code to produce useful scripts. This six-line script replaced a 60Kb executable (written in C) that did the same thing!
In Chapter 16 (p. 347), I will talk more about the dialback concept and show a different way to do it.
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.