$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 == "" } {