EXPECT(1) General Commands Manual EXPECT(1) (NAME) expect - (SYNOPSIS) expect [ -dDinN ] [ -c cmds ] [ [ -[f|b] ] cmdfile ] [ args ] (INTRODUCTION) Expect <<>> . Expect . . . Expectk Expect Tk . Expect wish Tk . Expect C C++ ( Tcl) . libexpect(3) . <> send/expect uucp kermit . uucp Expect . Expect . Expect : o . o ( rogue) ( ) . o fsck <> <> . o BBS ( MCI Mail CompuServe) . o rlogin telnet tip su chgrp . (shell) . ( .) Expect . Expect . . Expect ( ). . (USAGE) Expect cmdfile . Expect #! : #!/usr/local/bin/expect -f Expect . /usr/local/bin . -c . . . -c . . ( Expectk -command .) -d expect interact . <> Expect Expect . ( strace trace .) ( Expectk -diag .) -D (debugger) . . ^C ( ) Tcl . README << >> ( ) . ( Expectk -Debug .) -f . #! ( ) . ( Expectk -file .) . stdin . -b . ( Expectk -buffer .) stdio fifo stdin . <<->> . ( <<->> <<./->> .) -i Expect (prompt) . exit EOF . interpreter ( ) . -c -i . ( Expectk -interactive .).PP -- . Expect . #! Expect . ( ) argv . #!/usr/local/bin/expect -- #! getopt(3) execve(2) . $exp_library/expect.rc (source) -N . ( Expectk -NORC .) ~/.expect.rc -n . DOTDIR .expect.rc . ( Expectk -norc .) -c . -v Expect . ( Expectk -version .) args argv . argc argv . argv0 ( ) . : send_user "$argv0 [lrange $argv 0 2]\n" (COMMANDS) Expect Tcl (Tool Command Language) . Tcl ( if for break) (recursion) (procedure) . ( set if exec) Tcl ( tcl(3) ). Expect . . . spawn send expect interact . ( Expect Tcl) <> ( << (SEE ALSO)>> ). . ( <> <> Expect <> <> expect Expect .) close [-slave] [-onexec 0|1] [-i spawn_id] . EOF (stdin) close . -i spawn_id . expect interact close . <> close . -onexec spawn id (spawned) (overlay) . spawn id 0 . (). -slave (slave) spawn id . ( <> .) (slave) . wait (kernel process slot) . close wait . wait . debug [[-now] 0|1] Tcl (breakpoints) . 1 0 . 1 . 0 . 1 -now ( debug). Tcl . debug (traps) . Expect -D ( ). README << (SEE ALSO)>> ( ) . disconnect (forked) . . (process group) ( ). / /dev/null . disconnect . if {[fork]!=0} exit disconnect . . . . . ( stty .) send_user "password?\ " expect_user -re "(.*)\n" for {} 1 {} { if {[fork]!=0} {sleep 3600;continue} disconnect spawn priv_prog expect Password: send "$expect_out(1,string)\r" . . . exit } disconnect (&) Expect pty . & Expect Expect ..TP exit [-opts] [status] Expect . -onexit (exit handler) . . -noexit Expect . Expect . Expect . Expect Tcl . ( Tk) Tcl . exit Expect ( ) . (spawned) . EOF . exit _exit(2) . EOF . ( exit(3) .) init . status ( 0 ) Expect . exit . exp_continue [-continue_timer] exp_continue expect . exp_continue (timeout) . -continue_timer . ( expect .) exp_internal [-f file] value value Expect stderr . value 0 . . file ( value) . . -info exp_internal info . exp_open [args] [-i spawn_id] Tcl spawn id . open Tcl . ( spawn id . wait .) -leaveopen spawn id Expect . wait spawn id . exp_pid [-i spawn_id] (pid) . -i pid spawn id . exp_send send. exp_send_error send_error. exp_send_log send_log. exp_send_tty send_tty. exp_send_user send_user. exp_version [[-exit] version] Expect . Expect . . . . (major) . Expect . exp_version . (minor) . . exp_version Expect . . Expect ( ) . 0 . -exit Expect . expect [[-opts] pat1 body1] ... [-opts] patn [bodyn] (end- of-file) . . expect_before . expect_after . expect (backslash) . Tcl . eof . timeout (timeout) . timeout (null action) . "set timeout 30" . -1 . default . . expect ( ). . . ( prompt) . timeout ( )..IP . string match Tcl . ( C-shell <> ). -gl expect . <<->> . ( <<->> .) (login) . ( abort .) expect { busy {puts busy\n ; exp_continue} failed abort "invalid password" abort timeout abort connected } . ( ) . regexp ( ). glob Tcl . regexp regexp Tcl ( <>) . regexp -re . regexp : expect { busy {puts busy\n ; exp_continue} -re "failed|invalid password" abort timeout abort connected } <<>> (unanchored) . ( ). ^ $ . (echo) (spawned) . . $ . ^ $ . expect expect ( ) . ( <> .) -ex <<>> (exact) . * ^ ( Tcl ). . -nocase . . <<>> . match_max . ( .) patlist full_buffer match_max . full_buffer expect_out(buffer) . patlist null null ( remove_nulls) ASCII 0 . glob regexp . ( eof full_buffer) expect_out(buffer) . regexp expect_out(1,string) expect_out(9,string) . -indices ( lrange) expect_out(X,start) expect_out(X,end) X . glob regexp . "abcdefgh\n" : expect "cd" : set expect_out(0,string) cd set expect_out(buffer) abcd "efgh\n" . "abbbcabkkkka\n" : expect -indices -re "b(b*).*(k+)" : set expect_out(0,start) 1 set expect_out(0,end) 10 set expect_out(0,string) bbbcabkkkk set expect_out(1,start) 2 set expect_out(1,end) 3 set expect_out(1,string) bb set expect_out(2,start) 10 set expect_out(2,end) 10 set expect_out(2,string) k set expect_out(buffer) abbbcabkkkk "a\n" . "*" ( -re ".*") (flush) . Expect . -notransfer . ( "-not" ). spawn id ( eof full_buffer) expect_out(spawn_id) . -timeout expect timeout (timeout) . -i spawn_id ( -i ) . spawn_id spawn_id spawn_id . "connected" "busy" "failed" "invalid password" spawn_id proc2$ . expect { -i $proc2 busy {puts busy\n ; exp_continue} -re "failed|invalid password" abort timeout abort connected } any_spawn_id spawn_id -i expect . spawn_id -i ( -i ) expect any_spawn_id . -i spawn id . . / (I/O) . spawn id spawn id <<>> (indirect) . break continue ( for proc) . exp_continue expect ..IP expect . rlogin . exp_continue rlogin expect ( ) . expect { Password: { stty -echo send_user "password (for $user) on $host: " expect_user -re "(.*)\n" send_user "\n" send "$expect_out(1,string)\r" stty echo exp_continue } incorrect { send_user "invalid password or account\n" exit } timeout { send_user "connection to $host timed out\n" exit } eof { send_user \ "connection to host failed: $expect_out(buffer)" exit } -re $prompt } . raw . <<+>> . <

> (return) <> . exp_continue expect . stty raw -echo expect_after { -i $user_spawn_id "p" {send "\r\r\r"; exp_continue} "+" {incr foo; exp_continue} "i" {interact; exp_continue} "quit" exit } exp_continue (timeout timer) . exp_continue -continue_timer . expect_after [expect_args] expect_before expect expect_after expect . expect_before . expect_background [expect_args] expect . . timeout default expect_background . expect_background expect expect_before expect_after . expect_background spawn id . . expect () spawn id . expect expect_background . expect_background spawn id expect_background spawn id . expect_background spawn id . expect_before [expect_args] expect . - expect_before spawn id expect . expect (context) expect . expect_before expect expect_before . spawn id . -i expect_before spawn id expect_before ( ) . -info expect_before . spawn id . spawn id . : expect_before -info -i $proc spawn id . -indirect spawn id . spawn id <<-all>> <<-info>> spawn id . -info expect_before . expect_tty [expect_args] expect /dev/tty ( ) . cooked () . (return) expect . stty ( stty ). expect_user [expect_args] expect stdin ( ) . cooked () . (return) expect . stty ( stty ). fork . Expect . fork 0 () (process ID) . ( swap ) fork -1 ..IP (forked) exit . (log) . . pty . (spawning) fork . interact [string1 body1] ... [stringn [bodyn]] stdout stderr . - (string-body) . ( .) interpreter . interact backslash . Tcl . interact - : ^Z Expect . ( -reset .) ^A "you typed a control-A" ^A . $ . ^C Expect . "foo" "bar" . ~~ Expect . set CTRLZ \032 interact { -reset $CTRLZ {exec kill -STOP [pid]} \001 {send_user "you typed a control-A\n"; send "\001" } $ {send_user "The date is [clock format [clock seconds]]."} \003 exit foo {send_user "bar"} ~~ } - . . . . (wildcard) . ( expect glob .) -ex interact . "-" . ( "-" .) -re (regexp) . interact_out expect expect_out . -indices . eof (end-of-file) . eof -output eof . eof "return" interact EOF . timeout ( ) . timeout . . "timeout" ( expect) . (autologout) : interact -input $user_spawn_id timeout 3600 return -output \ $spawn_id null null ( remove_nulls) 0 ASCII . glob regexp . -iwrite interact_out(spawn_id) spawn_id ( eof) . break continue ( for proc) . return interact inter_return interact . "proc foo" interact inter_return proc foo . ( interact interpreter return interact inter_return interact .) interact raw . (job control) ( ^Z) . ( "kill -CONT "). SIGSTOP ( ^Z) csh . SIGSTOP Expect interpreter ( ) ^Z . - . - . raw . -reset interact ( cooked). ( ). -reset cooked ..IP -echo . . (echo) . . -echo . rftp ( ftp ) (get) (put) (list) ~g ~p ~l . ftp ~ . interact { -echo ~g {getcurdirectory 1} -echo ~l {getcurdirectory 0} -echo ~p {putcurdirectory} } -nobuffer . . ( Hayes) . "atd" . proc lognumber {} { interact -nobuffer -re "(.*)\r" return puts $log "[clock format [clock seconds]]: dialed $interact_out(1,string)" } interact -nobuffer "atd" lognumber interact log_user . interact ( ) . -o - (key-body) . telnet . interact stdin stdout Expect . -u ( "user") interact ( spawned id ). . Expect stderr ( stdout). interpreter stdin . login . ( ) . login . . spawn login set login $spawn_id spawn tip modem # dial back out to user # connect user to login interact -u $login spawn id -output . spawn id spawn id -input . ( -input -output -i expect any_spawn_id interact .) ( ) -input . -input -output "-input $user_spawn_id -output" . ( -input .) -input $user_spawn_id . -input $spawn_id . -input . $spawn_id $user_spawn_id ( ) . -input -output . -i -input -output spawn_id . -i -o . spawn id . ( spawn id expect .) spawn id -i -u -input -output . interpreter [args] Expect Tcl . . break continue ( for proc) . return interpreter inter_return interpreter . "proc foo" interpreter inter_return proc foo . interpreter . (prompt) . ( Tcl_Eval ). Tcl . "prompt1" . ( "+> ") . "prompt2" . interpreter (cooked mode) (raw mode) . stdin interpreter -eof ..TP log_file [args] [[-a] file] log_file ( ) . log_file . . -open -leaveopen Tcl . spawn . ( spawn .) -a log_user . log_file . -noappend . -info log_file -info . log_user -info|0|1 send/expect stdout ( ) . stdout "log_user 0" "log_user 1" . . -info log_user -info . match_max [-d] [-i spawn_id] [size] ( ) expect . size . -d . ( .) -i spawn id . overlay [-# spawn_id] [-# spawn_id] [...] program [args] program args Expect . (bare hyphen) (login shell) . spawn_id . . spawn_id . chess - . overlay -0 $spawn_id -1 $spawn_id -2 $spawn_id chess "interact -u" Expect . . (job control) ( login ) . parity [-d] [-i spawn_id] [value] (parity) . value . value . -d . ( .) -i spawn id . remove_nulls [-d] [-i spawn_id] [value] (null) expect_out interact_out . value . value . value . -d . ( .) -i spawn id . Expect null stdout . send [-flags] string string . send "hello world\r" h e l l o w o r l d . ( Tcl -printf ( format) .) (line-buffered) return . return "\r" . -- . "--" . . ( "-" .) -i spawn_id . spawn_id user_spawn_id raw (newline) return- newline cooked . -raw . -null null ( ) . null . -null null . -break break . spawn id tty "spawn -open" . tip tip break . -s "" ( ) . "send_slow" . () . . "set send_slow {10 .001}" "send -s" . -h ( ) . . ( (Weibull) .) "send_human" . (interarrival time) . . . . . . (clip) . . : set send_human {.1 .3 1 .05 2} send -h "I'm hungry. Let's do lunch." : set send_human {.4 .4 .2 .5 100} send -h "Goodd party lash night!" send . null break . . string null break . send expect . expect send . send . (prompt) send : # To avoid giving hackers hints on how to break in, # this system does not prompt for an external password. # Wait for 5 seconds for exec to complete spawn telnet very.secure.gov sleep 5 send password\r exp_send send . Expectk Expect Tk send Tk . exp_send . send Expect ..TP send_error [-flags] string send stderr . send_log [--] string send ( log_file ). . send_tty [-flags] string send /dev/tty . send_user [-flags] string send stdout . sleep seconds . . ( Expectk Tk) Expect . spawn [args] program [args] program args . (stdin) (stdout) (stderr) Expect Expect . close . spawn spawn_id . spawn_id . spawn_id . user_spawn_id . spawn_id expect expect_user . error_spawn_id . spawn_id send send_error . tty_spawn_id /dev/tty . /dev/tty ( cron at batch) tty_spawn_id . : if {[info vars tty_spawn_id]} { # /dev/tty exists } else { # /dev/tty doesn't exist # probably in cron, batch, or at script } spawn . 0 . spawn_out(slave,name) pty . spawn (echo) . -noecho spawn . -console . . spawn pty tty . "" ( stty(1)) . stty_init stty . "set stty_init raw" (raw) . -nottycopy tty . -nottyinit "" . spawn . spawn pty (wedged) . pty . ( pty .) Expect -d Expect pty . pty (reboot) . program exec(2) ( program ) interact expect program . spawn . spawn fork Expect spawn_id . -open Tcl ( open) . spawn . ( .) pty . 0 . Tcl . -leaveopen -open -leaveopen spawn . -pty pty . 0 . Spawn_id . spawn_out(slave,fd) pty . "close -slave" . -ignore . . trap . strace level . ( trace Tcl .) level (call stack) . Expect . expect -c "strace 4" script.exp -info strace info ..TP stty args stty . . <<< /dev/tty...>> . ( .) . raw echo . raw -cooked raw . -raw cooked cooked . echo -echo echo noecho . (echoing) . . ( EXPECT HINTS .) stty -echo send_user "Password: " expect_user -re "(.*)\n" set password $expect_out(1,string) stty echo system args args sh(1) . Expect . sh exec . exec stdin stdout system ( ). /dev/tty . system (log) . timestamp [args] (timestamp) . (epoch) . -format POSIX strftime . %a ( Sat) . : %a %A %b %B %c - : Wed Oct 6 11:45:56 1993 %d (01-31) %H (00-23) %I (01-12) %j (001-366) %m (01-12) %M (00-59) %p am pm %S (00-61) %u (1-7 ) %U (00-53 ) %V (01-53 ISO 8601) %w (0-6) %W (00-53 ) %x : Wed Oct 6 1993 %X : 23:59:59 %y (00-99) %Y : 1993 %Z ( ) %% % . . C . -seconds epoch . . -gmt GMT . . trap [[command] signals] command . (global scope) . command . command SIG_IGN . command SIG_DFL . signals . signal(3) . <> . ( -number) trap . -code Tcl . -interp . -name trap . -max trap . <> ^C "Ouch!" . SIGINT ( ^C ) SIGTERM Expect . Expect . trap exit {SIGINT SIGTERM} -D (debugger) SIGINT . : trap {exp_debug 1} SIGINT EXPECT_DEBUG_INIT . trap . <> . . SIGINT : if {![exp_debug]} {trap mystuff SIGINT} . trap SIGALRM Expect . disconnect SIGALRM SIG_IGN () . spawn . signal(3) ..TP wait [args] ( ) . wait . pid . spawn id . -1 0 . 0 (status) . -1 errno . errorCode . wait . . CHILDKILLED C . -i spawn_id ( process id). SIGCHLD spawn id -1 . -nowait . () . wait "-i -1" . . (reap) . . (LIBRARIES) Expect Expect . exp_library exp_exec_library . . exp_library . exp_exec_library . . $exp_exec_library/cat- buffers /bin/cat (buffer) . (PRETTY-PRINTING) vgrind (pretty-printing) Expect . vgrind Expect : vgrind -lexpect file (EXAMPLES) . example Expect . . . INSTALL . Expect ( SEE ALSO ) . Expect . (CAVEATS) Expect . send Tk . Expect "exp_XXXX" . "exp" "inter" "spawn" "timeout" (alias) . . Expect (scoping) . Expect . "global timeout" expect . ( "global" ). spawn . spawn_id . "global spawn_id" . (multispawning) ( select (BSD *.*) poll (SVR>2) ) Expect . spawn_id exec . ( ) expect . . (echoing) . Expect (sane) . . emacs <<>> : carriage-return newlines newlines . emacs . Expect . Expect . emacs . ( expect interact) (heuristic) . \n . "-nobrace" . Expect . -brace / ..SH " (BUGS)" <> ( <> <>) ( ) . (spawn) tty . tty Expect . . Ultrix 4.1 ( ) (timeout) 1000000 0 . Digital UNIX 4.0A ( ) SIGCHLD pty . grantpt . IRIX 6.0 pty Expect pty . IRIX 6.1 . Telnet ( SunOS 4.1.2 ) TERM (hang) . cron at cgi TERM . - ! . set env(TERM) vt100 Tip ( BSDI BSD/OS 3.1 i386 ) SHELL HOME . cron at cgi . - ! . set env(SHELL) /bin/sh set env(HOME) /usr/local/bin pty ( ) . Expect spawn date sleep 20 expect . spawn exec . . pty Cray UNICOS . Cray . (prompt) tty UART (baud rate) start/stop . (sleep) . . : send "speed 9600\r"; sleep 1 expect { timeout {send "\r"; exp_continue} $prompt } trap -code Tcl ( sleep) . Tcl (async) . trap ( sleep) . expect_background -timeout . EXPECT (EXPECT HINTS) Expect . . expect (shell prompts) . rlogin . (regular expression) ( ) EXPECT_PROMPT . . EXPECT_PROMPT . set prompt "(%|#|\\$) $" ;# default prompt catch {set prompt $env(EXPECT_PROMPT)} expect -re $prompt expect . . . . . ftp 'f' 't' 'p' '>' () . . . . X* * X . << >> I/O . ( ) . * . expect . . . . timeout . expect (timeout) - eof . . . (newlines) carriage return linefeed . printf("foo\nbar") "foo\r\nbar" . expect_user . return newline . Expect raw ( telnet) return . ( newline return .) raw . newline return "stty raw" . (cooked) . interact raw . ( ) Expect . . . ..PP UNIX . setgid : Expect ( ) . 750 (-rwxr-x---) . . /bin/sh 2751 (-rwxr-s--x) . ( ) . Expect . (SEE ALSO) Tcl(3), libexpect(3) "Exploring Expect: A Tcl-Based Toolkit for Automating Interactive Programs" Don Libes . ISBN 1-56592-090-2 O'Reilly and Associates . "expect: Curing Those Uncontrollable Fits of Interactivity" Don Libes Proceedings of the Summer 1990 USENIX Conference, Anaheim, California, June 11-15, 1990. "Using expect to Automate System Administration Tasks" Don Libes Proceedings of the 1990 USENIX Large Installation Systems Administration Conference, Colorado Springs, Colorado, October 17-19, 1990. "Tcl: An Embeddable Command Language" John Ousterhout Proceedings of the Winter 1990 USENIX Conference, Washington, D.C., January 22-26, 1990. "expect: Scripts for Controlling Interactive Programs" Don Libes Computing Systems, Vol. 4, No. 2, University of California Press Journals, November 1991. "Regression Testing and Conformance Testing Interactive Programs", Don Libes, Proceedings of the Summer 1992 USENIX Conference, pp. 135-144, San Antonio, TX, June 12-15, 1992. "Kibitz - Connecting Multiple Interactive Programs Together", Don Libes Software - Practice & Experience, John Wiley & Sons, West Sussex, England, Vol. 23, No. 5, May, 1993. "A Debugger for Tcl Applications", Don Libes Proceedings of the 1993 Tcl/Tk Workshop, Berkeley, CA, June 10-11, 1993. (AUTHOR) Don Libes (NIST) (ACKNOWLEDGMENTS) John Ousterhout Tcl Scott Paisley . Rob Savoye Expect. HISTORY expect . . . Expect (public domain) . NIST . 29 December 1994 EXPECT(1)