puts "Usage: <user> <passw> <host> <location> <file1 to send> <file2 to send>\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 Alternatives Various projects implement Expect-like functionality in other languages, such as C#, Java, Scala, Groovy, Perl, Python, Ruby, Shell and Go. These are generally not exact clones of the original Expect, but the concepts tend to be very similar. C# Expect.NET — Expect functionality for C# (.NET) DotNetExpect — An Expect-inspired console automation library for .NET Erlang lux - test automation framework with Expect style execution commands. Go GoExpect - Expect-like package for the Go language go-expect - an Expect-like Go language library to automate control of terminal or console based programs. Groovy expect4groovy - a Groovy DSL implementation of Expect tool. Java ExpectIt — a pure Java 1.6+ implementation of the Expect tool. It is designed to be simple, easy to use and extensible. expect4j — an attempt at a Java clone of the original Expect ExpectJ — a Java implementation of the Unix expect utility Expect-for-Java — pure Java implementation of the Expect tool expect4java - a Java implementation of the Expect tool, but supports nested closures. There is also wrapper