1AUTOEXPECT(1) General Commands Manual AUTOEXPECT(1)
2
3
4
6 autoexpect - generate an Expect script from watching a session
7
9 autoexpect [ args ] [ program args... ]
10
12 autoexpect watches you interacting with another program and creates an
13 Expect script that reproduces your interactions. For straightline
14 scripts, autoexpect saves substantial time over writing scripts by
15 hand. Even if you are an Expect expert, you will find it convenient to
16 use autoexpect to automate the more mindless parts of interactions. It
17 is much easier to cut/paste hunks of autoexpect scripts together than
18 to write them from scratch. And if you are a beginner, you may be able
19 to get away with learning nothing more about Expect than how to call
20 autoexpect.
21
22 The simplest way to use autoexpect is to call it from the command line
23 with no arguments. For example:
24
25 % autoexpect
26
27 By default, autoexpect spawns a shell for you. Given a program name
28 and arguments, autoexpect spawns that program. For example:
29
30 % autoexpect ftp ftp.cme.nist.gov
31
32 Once your spawned program is running, interact normally. When you have
33 exited the shell (or program that you specified), autoexpect will cre‐
34 ate a new script for you. By default, autoexpect writes the new script
35 to "script.exp". You can override this with the -f flag followed by a
36 new script name.
37
38 The following example runs "ftp ftp.cme.nist.gov" and stores the
39 resulting Expect script in the file "nist".
40
41 % autoexpect -f nist ftp ftp.cme.nist.gov
42
43 It is important to understand that autoexpect does not guarantee a
44 working script because it necessarily has to guess about certain things
45 - and occasionally it guesses wrong. However, it is usually very easy
46 to identify and fix these problems. The typical problems are:
47
48 · Timing. A surprisingly large number of programs (rn, ksh,
49 zsh, telnet, etc.) and devices (e.g., modems) ignore key‐
50 strokes that arrive "too quickly" after prompts. If you
51 find your new script hanging up at one spot, try adding a
52 short sleep just before the previous send.
53
54 You can force this behavior throughout by overriding the
55 variable "force_conservative" near the beginning of the gen‐
56 erated script. This "conservative" mode makes autoexpect
57 automatically pause briefly (one tenth of a second) before
58 sending each character. This pacifies every program I know
59 of.
60
61 This conservative mode is useful if you just want to quickly
62 reassure yourself that the problem is a timing one (or if
63 you really don't care about how fast the script runs). This
64 same mode can be forced before script generation by using
65 the -c flag.
66
67 Fortunately, these timing spots are rare. For example, tel‐
68 net ignores characters only after entering its escape
69 sequence. Modems only ignore characters immediately after
70 connecting to them for the first time. A few programs
71 exhibit this behavior all the time but typically have a
72 switch to disable it. For example, rn's -T flag disables
73 this behavior.
74
75 The following example starts autoexpect in conservative
76 mode.
77
78 autoexpect -c
79
80 The -C flag defines a key to toggle conservative mode. The
81 following example starts autoexpect (in non-conservative
82 mode) with ^L as the toggle. (Note that the ^L is entered
83 literally - i.e., enter a real control-L).
84
85 autoexpect -C ^L
86
87 The following example starts autoexpect in conservative mode
88 with ^L as the toggle.
89
90 autoexpect -c -C ^L
91
92
93 · Echoing. Many program echo characters. For example, if you
94 type "more" to a shell, what autoexpect actually sees is:
95
96 you typed 'm',
97 computer typed 'm',
98 you typed 'o',
99 computer typed 'o',
100 you typed 'r',
101 computer typed 'r',
102 ...
103
104 Without specific knowledge of the program, it is impossible
105 to know if you are waiting to see each character echoed
106 before typing the next. If autoexpect sees characters being
107 echoed, it assumes that it can send them all as a group
108 rather than interleaving them the way they originally
109 appeared. This makes the script more pleasant to read.
110 However, it could conceivably be incorrect if you really had
111 to wait to see each character echoed.
112
113
114 · Change. Autoexpect records every character from the inter‐
115 action in the script. This is desirable because it gives
116 you the ability to make judgements about what is important
117 and what can be replaced with a pattern match.
118
119 On the other hand, if you use commands whose output differs
120 from run to run, the generated scripts are not going to be
121 correct. For example, the "date" command always produces
122 different output. So using the date command while running
123 autoexpect is a sure way to produce a script that will
124 require editing in order for it to work.
125
126 The -p flag puts autoexpect into "prompt mode". In this
127 mode, autoexpect will only look for the last line of program
128 output - which is usually the prompt. This handles the date
129 problem (see above) and most others.
130
131 The following example starts autoexpect in prompt mode.
132
133 autoexpect -p
134
135 The -P flag defines a key to toggle prompt mode. The fol‐
136 lowing example starts autoexpect (in non-prompt mode) with
137 ^P as the toggle. Note that the ^P is entered literally -
138 i.e., enter a real control-P.
139
140 autoexpect -P ^P
141
142 The following example starts autoexpect in prompt mode with
143 ^P as the toggle.
144
145 autoexpect -p -P ^P
146
147
149 The -quiet flag disables informational messages produced by autoexpect.
150
151 The -Q flag names a quote character which can be used to enter charac‐
152 ters that autoexpect would otherwise consume because they are used as
153 toggles.
154
155 The following example shows a number of flags with quote used to pro‐
156 vide a way of entering the toggles literally.
157
158 autoexpect -P ^P -C ^L -Q ^Q
159
160
162 I don't know if there is a "style" for Expect programs but autoexpect
163 should definitely not be held up as any model of style. For example,
164 autoexpect uses features of Expect that are intended specifically for
165 computer-generated scripting. So don't try to faithfully write scripts
166 that appear as if they were generated by autoexpect. This is not use‐
167 ful.
168
169 On the other hand, autoexpect scripts do show some worthwhile things.
170 For example, you can see how any string must be quoted in order to use
171 it in a Tcl script simply by running the strings through autoexpect.
172
173
175 "Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Pro‐
176 grams" by Don Libes, O'Reilly and Associates, January 1995.
177
179 Don Libes, National Institute of Standards and Technology
180
181 expect and autoexpect are in the public domain. NIST and I would
182 appreciate credit if these programs or parts of them are used.
183
184
185
186
187 30 June 1995 AUTOEXPECT(1)