is a general-purpose scripting language that is used to
control the modem, dial the remote server, and perform the remote system
is less powerful than
but is widely used.
The "expect/send" structure of a
script is the fundamental
structure used in most scripting languages.
script is composed of expect/send pairs. These pairs
consist of the string expected from the remote system, separated by
whitespace from the response that is sent to the remote host when the
expected string is received. If no string is expected from the remote
system, two quotes (
) or two apostrophes (
used to "expect nothing." A simple
"" \r name> jane word> TOga!toGA
The script expects nothing (
) until it sends the remote
system a carriage return (
). Then the script expects the
remote system to send the string
, which is part of the
prompt. In response to this prompt, the
script sends the username
. Finally the script waits for
part of the
prompt and responds with
. A script this simple can be defined directly on the
% chat -v -t30 "" \r name> jane word> TOga!toGA
This command runs
in verbose mode, sets the length of time
the script waits for an expected string to 30 seconds, and then executes
the simple login script described above.
The syntax of the
command options are:
script in verbose mode. Verbose mode logs
informational messages via
script in stderr verbose mode. The stderr verbose
mode displays informational messages on the stderr device. See Chapter
6 for an example of this being used with
Sets the maximum time to wait for an expected string. If the expected
string is not received in
seconds, the reply string is
not sent and the script terminates&amp;amp;mdash;unless an alternate send is
defined. If defined, the alternate send (more about this later) is
sent and the remote system is given one more
respond. If this fails, the script is terminated with a nonzero error
code. By default, the timeout period is 45 seconds.
script from the
instead of from
the command line. Multiple lines of expect/send pairs are permitted in
Writes the output generated by REPORT strings to the
By default, REPORT strings are written to stderr. The REPORT keyword is
In order to make the scripts more useful and robust,
provides special keywords, escape sequences, and alternate send/expect
pairs that can be used in the script. First let's look at the five
Two keywords transmit special signals to the remote system. The
sends the End of Transmission character. On UNIX
systems this is usually the End of File character, which is a
keyword sends a line break to the remote
system. The three remaining keywords define processing characteristics
for the script itself.
keyword defines the amount of time to wait for an expected
string. Because it is defined inside the script, the timeout value can
be changed for each expected string. For example, assume you want to
allow the remote server 30 seconds to display the initial
prompt but only 5 seconds to display
once the username has
been sent. Enter this script command:
TIMEOUT 30 name> karen TIMEOUT 5 word> beach%PARTY
keyword and the
similar. They both define strings that, when received, cause a special
action to take place. The
keyword defines strings that
cause the script to abort if they are received when the system is
expecting the string CONNECT from the modem. The
keyword defines substrings that determine what messages received on
the serial port should be written to stderr or the report file. A
script file illustrates both of these keywords:
ABORT 'NO CARRIER'
ABORT 'RING - NO ANSWER'
The first line says that any message received by the script that
contains the word CONNECT will be logged. If the
command-line option was used when
was started, the message
is logged in the file defined by that option. Otherwise the message is
displayed on stderr. The point of this command is to display the
modem's connect message to the user. For example: the complete message
might be CONNECT 28,800 LAPM/V, which tells the user the link speed
and the transmission protocol used by the modems. The CONNECT message
means success. The next three lines of the script begin with the
and define the modem messages that mean failure. If the
modem responds with BUSY, NO CARRIER, or RING - NO ANSWER, the
The last four lines are the basic expect/send pairs we have seen
repeatedly in this section. We expect nothing (
) and send the
dial command to the modem (
). We expect CONNECT from the
modem and send a carriage return (
) to the remote server.
from the remote server and send
Finally, we expect
from the server and send
extends the standard expect/send pair with an alternate send
and an alternate expect to improve robustness. You may define an
alternate send string and an alternate expect value to be used when the
script times out waiting for the primary expected value. The alternate
send and the alternate expect are indicated in the script by preceding
them with dashes. For example:
In this sample we wait for the string
and send the string
. The first string and the last string compose the
standard expect/send pair. The alternate send/expect is only used if
the timer expires and the expected
string has not been
received. When this occurs, the script sends a line break, restarts
the timer, and waits for
again, because that is what our
alternate send/expect pair (
) tells the script to
do. Note that unlike the standard expect/send pair, in the send/expect
pair a value is transmitted before a string is expected, i.e., the send
comes before the expect. Another example more in keeping with our
other script examples is:
Here the script expects the
string. If it is not
received, the script sends an empty line, which is simply a carriage
return, and again waits for the
string. This action is
dictated by the alternate send/expect pair,
pair begins with a dash that signals the start of the send string, but
the next character is the second dash that marks the beginning of the
alternate expect string. There is no send string. It is this "empty
string" that causes the script to send a single return character. This
example is more common than the BREAK example shown above, though a little
harder to explain.
The carriage return character is not the only special character that can be sent from
provides several escape sequences for
sending and receiving special characters.
Table A.2: chat Escape Sequences
The backspace character.
Send without the terminating return character.
Delay sending for one second.
Send a BREAK.
Send a newline character.
Send a null character.
Delay sending 1/10th of a second.
Send the string but don't log it.
The carriage return.
The space character.
The tab character.
The backslash character.
The ASCII character with the octal value ddd.
A control character.
All of the escape sequences start with a backslash (\)
except for the sequence
used to enter a control character. Control characters are entered as a
caret (^) followed by an uppercase letter. For example control X is entered
as ^X. The escape
sequences that are described in
with the words "send" or
"sending" can only be used in a send string; all others can be used in
either a send or expect string. Several escape sequences are used in
the following example:
Expect nothing (
). Wait two seconds (
three ASCII BELL characters, which is CTRL-G on the keyboard, at
intervals of 1/10 of a second (
). Send the
. Go to a new line (
) and send the