Automating SFTP with Expect

Hello all,
I've written an automated SFTP script to work with the Expect command. It recently occurred to me however, that if the client side box does not have the known host entry for the server, it will not work correctly. So I have added an expect for the known host prompt, and that part works correctly, however, it does not seem to continue past that point to enter the password successfully if the known host file has already been configured for that particular server. I'm assuming I need some sort of timeout for the expect of the known hosts warning, or an if of sorts. I'm not sure how to go about this with expect though. Any help would be great. :slight_smile: Below is my script:

#!/usr/bin/expect
set SYSTEM [lindex $argv 0]
set USER [lindex $argv 1]
set PASSWORD [lindex $argv 2]

set AUTOHOME "/etc/scripts/automation"
set TRIGDIR "/etc/scripts/automation/.engine_triggers"
set CTRIGDIR "/etc/scripts/automation/.client_triggers"

spawn /usr/bin/sftp $USER@$SYSTEM
expect "Are you sure you want to continue connecting (yes/no)?"
send "yes \r"
expect "password:"
send "$PASSWORD \r"
expect "sftp> "
send "put $CTRIGDIR/command $TRIGDIR \r"
expect "sftp> "
send "bye \r"
exit 0

If you want the expect script to pause / wait for a certain amount of time
then just add

sleep 1

at the appropriate position

This will cause the prg to wait for 1 second.

You could also force expect into conservative mode, read the man page
on your system for information on this.

This will make the prg wait 1/10 sec between each character being sent.

Well, it doesn't seem to need a pause as far as I can tell. It seems more like it's looking for the Continue yes/no text, but if the key is already on that system, it never shows up and doesn't continue to input the password.