Expect
Expect is an extension to the Tcl scripting language written by Don Libes. The program automates interactions with programs that expose a text terminal interface. Expect, originally written in 1990 for the Unix platform, has since become available for Microsoft Windows and other systems.
Expect is an extension to the Tcl scripting language written by Don Libes. The program automates interactions with programs that expose a text terminal interface. Expect, originally written in 1990 for the Unix platform, has since become available for Microsoft Windows and other systems. Assume $remote_server, $my_user_id, $my_password, and $my_command were read earlier in the script. Open a Telnet session to a remote server, and wait for a username prompt. spawn telnet $remote_server expect "username:" Send the username, and then wait for a password prompt. send "$my_user_id\r" expect "password:" Send the password, and then wait for a shell prompt. send "$my_password\r" expect "%" Send the prebuilt command, and then wait for another shell prompt. send "$my_command\r" expect "%" Capture the results of the command into a variable. This can be displayed, or written to disk. set results $expect_out(buffer) Exit the Telnet session, and wait for a special end-of-file character. send "exit\r" expect eof Another example is a script that automates FTP: Set timeout parameter to a proper value. For example, the file size is indeed big and the network speed is really one problem, you'd better set this parameter a value. set timeout -1 Open an FTP session to a remote server, and wait for a username prompt. spawn ftp $remote_server expect "username:" Send the username, and then wait for a password prompt. send "$my_user_id\r" expect "password:" Send the password, and then wait for an 'ftp' prompt. send "$my_password\r" expect "ftp>" Switch to binary mode, and then wait for an 'ftp' prompt. send "bin\r" expect "ftp>" Turn off prompting. send "prompt\r" expect "ftp>" Get all the files send "mget *\r" expect "ftp>" Exit the FTP session, and wait for a special end-of-file character. send "bye\r" expect eof Below is an example that automates SFTP (with a password): !/usr/bin/env expect -f Procedure to attempt connecting; result 0 if OK, 1 otherwise proc connect {passw} { expect { "Password:" { send "$passw\r" expect { "sftp*" { return 0 } } } } # Timed out return 1 } Read the input parameters set user [lindex $argv 0] set passw [lindex $argv 1] set host [lindex $argv 2] set location [lindex $argv 3] set file1 [lindex $argv 4] set file2 [lindex $argv 5] puts "Argument data:\n"; puts "user: $user"; puts "passw: $passw"; puts "host: $host"; puts "location: $location"; puts "file1: $file1"; puts "file2: $file2"; Check if all were provided if { $user == "" || $passw == "" || $host == "" || $location == "" || $file1 == "" || $file2 == "" } { puts "Usage: \n" exit 1 } Sftp to specified host and send the files spawn sftp $user@$host set rez [connect $passw] if { $rez == 0 } { send "cd $location\r" set timeout -1 send "put $file2\r" send "put $file1\r" send "ls -l\r" send "quit\r" expect eof exit 0 } puts "\nError connecting to server: $host, user: $user and password: $passw!\n" exit 1 Using passwords as command-line arguments, like in this example, is a huge security hole, as any other user on the machine can read this password by running "ps". You can, however, add code that will prompt you for your password rather than giving your password as an argument. This should be more secure. See the example below. stty -echo send_user -- "Enter Password: " expect_user -re "(.*)\n" send_user "\n" stty echo set PASS $expect_out(1,string) Another example of automated SSH login to a user machine: Timeout is a predefined variable in Expect which by default is set to 10 seconds. spawn_id is another predefined variable in Expect. It is a good practice to close spawn_id handle created by spawn command. set timeout 60 spawn ssh $user@machine while {1} { expect { eof {break} "The authenticity of host" {send "yes\r"} "password:" {send "$password\r"} "*\]" {send "exit\r"} } } wait close $spawn_id
References
No reference rows published for this article.
Cite this article
MLA
"Expect." encyclopegia.org, 2026, https://encyclopegia.org/wiki/expect. Accessed 24 November 2026.
Chicago
"Expect." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/expect.
APA
encyclopegia.org. (2026). Expect. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/expect
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON