Expect Automate interactive programs
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
spawn <cmd> [args]
expect "<pattern>"
send "<text>\r"
interactset timeout <sec>
expect eofexpect -f <script>
autoexpect <cmd>
Concepts
- spawn start program on a pty
- expect match pattern
- send type the reply
- interact / eof hand over or finish
Running
expect <script> [args]
expect -d <script>
#!/usr/bin/env expect
autoexpect [-f <out>] <cmd>
unbuffer <cmd>
Spawn and send
spawn <cmd> [args]
spawn -noecho <cmd>
send "<text>\r"
send -- "<text>"
send -s "<text>"
send -i $<id> "<text>"
send_user "<text>"
set <id> $spawn_id
waitexit [<status>]
sleep <sec>
Expect patterns
expect "<glob>"
expect -re "<regexp>"
expect -ex "<text>"
expect eofexpect timeout { <cmds> }
expect { "a" {<cmds>} "b"
{<cmds>} }
exp_continueexpect -timeout <sec> "<pat>"
expect_before { <pat> {<cmds>} }
Variables
set timeout <sec>
$expect_out(buffer)
$expect_out(0,string)
$expect_out(1,string)
$spawn_id$argv / $argc / $argv0
lindex $argv <n>
$env(<NAME>)
Interact
interactinteract { "<key>" { <cmds> } }
interact timeout <sec> { <cmds> }
returnlog_user 0stty -echoexpect_user -re "(.*)\n"
Logging, debug and signals
exp_internal 1log_file <file>
Tcl essentials
set <var> <value>
foreach h $hosts { ... }
proc <name> {<args>} { ... }
regexp {<re>} $str -> a b
[exec <cmd>]
Recipes
set timeout 20spawn ssh <user>@<host>expect { "yes/no" { send "yes\r" exp_continue } "assword:" { send "$env(PW)\r" }}interact
spawn ssh <host> uptimeexpect -re {average: ([0-9.]+)}puts $expect_out(1,string)expect eof
spawn <cmd>expect eoflassign [wait] pid id os rcexit $rc



