Expect - A Tool For Automating Interactive Programs: Don Libes Libes@nist - Gov
Expect - A Tool For Automating Interactive Programs: Don Libes Libes@nist - Gov
Don Libes
libes@nist.gov
% telnet medlib.iaims.georgetown.edu
Trying 141.161.42.1 ...
Connected to medlib.iaims.georgetown.edu.
Escape character is ‘^]’.
UNIX(r) System V Release 4.0 (medlib)
login: medlib
Enter Your Last Name: mulroney
Password:XXXXXXXX
... menu ...
Enter a number or Q to QUIT: 4
• Motivation
• Basics, Examples
• Patterns and Actions
• Autoexpect
• Total automation vs partial automation
• Converting line-oriented apps to window-apps
• Background: CGI, cron, etc
• Security
• Esoterica
• URLs and other pointers
• Send a string
send "dir\r"
send "get $file\r"
set temperature 20
expect {
"pattern1" action1
"pattern2" action2
"pattern3" {
action3a
action3b
action3c
}
}
expect {
"does not exist" exit
"password: " {
send "$password\r"
# more stuff can done here
}
}
expect {
-re "3.*ftp>" action3
-re "2.*ftp>" action2
-gl "ftp>" actionDefault
}
set timeout 60
set timeout -1 ;# no timeout
while {1} {
expect {
eof break
-re "UNREF FILE.*CLEAR.*?" {send"y\r"}
-re "BAD INODE.*FIX.*?" {send "n\r"}
-re "? " {interact}
}
}
while {1} {
expect {
eof break
-re "UNREF FILE.*CLEAR.*?" {send"y\r"}
-re "BAD INODE.*FIX.*?" {send "n\r"}
-re "? " {
interact "+" return
}
}
}
interact {
"~g\r" {get_current_directory}
"~p\r" {put_current_directory}
"~l\r" {list_current_directory}
}
proc list_current_directory {} {
send "dir\r"
# expect commands to read directory
expect {
"ld password:" {
send "$oldpass\r"
} "assword*:" {
send "$newpass\r"
} -re "(.*)\n" {
showerr "$expect_out(1,string)"
} eof {
showerr "passwd died unexpectedly"
}
• Global patterns
• expect_before
• expect_after
• Multiple processes
• Example: testing two programs
• Example: program1 -> program2 -> program1
Expectk
Expect Tk original
Tcl Xlib process
script
sort
rand
passwd
password
databases
help
dictionaries
generated
Expect
script
interactive processes
% autoexpect
autoexpect started, file is script.exp
... interact ...
autoexpect done, file is script.exp
%
• xterm
• send: output appears in xterm for user to see
• expect: reads user keystrokes
• xterm itself takes care of everything else: character graphics, select, mouse, scroll
• good for applications that demand an xterm
• tkterm
• Expect and Tk take care of everything
• essentially a terminal widget
• with enhanced expect command
• good for Curses-based applications
• For most applications, tkterm is the way to go
• much more flexible
• http://expect.nist.gov/cgi.tcl
• Generally bad
• but so useful
• easy to avoid in Expect
# go into background
if {[fork]} exit
disconnect
sleep ...
spawn telnet ...
expect ...
send ...
Expect: Introduction and Overview Don Libes 42
Variations
while {1} {
send "$password\r"
expect {
"sorry" {
find-human
set passwd [getpass-from-human]
}
"$prompt" break
}
}
“Expect has become a necessary tool for system administration. In a short time,
we have used Expect in six areas and have cut out seven hours a week in tedious
and repetitive tasks.”
—Thomas Naughton, Hull Trading Company
“Expect is a lifesaver for a project that I am currently involved with. I have only
been working with Expect for the last couple of days, but it has already shaved
about 6 months off of the completion time of the project.”
—Ron Young, System Computing Services, University of Nevada
“Thanks for making my life easier. This program has really helped me shorten
the cycle time for software Q.A. Expect is like a dream come true for automa-
tion. My productivity has really increased.”
—Brian F. Woodson, 3Com NSD Software Q.A.
“I figure we saved about $35K last year (Jan-Dec94) that was directly attribut-
able to Expect. The indirect benefits drive that figure to more like $75K.”
—John Pierce, Chem Dept, UC San Diego
• Easy to get
• Cost: Free
• URL: http://expect.nist.gov
• Easy to install
• Portable
• UNIX: GNU-style configure
• Windows: ports from Cygnus and Berkeley
• Mac: sorry
• Well documented
• Numerous published papers
• Comprehensive man pages
• Exploring Expect (O’Reilly), ISBN: 1-56592-090-2
• Commercial Support Available
• Scriptics
• Cygnus Software
• Computerized Processes Unlimited