Promptless SFTP in bash script

Want to run SFTP command in a bash script without being prompted? Here are quick examples of 2 approaches; one with password based authentication and one with private key authentication.

Password Authentication

cd /source_directory_on_local

echo "cd /target_directory_on_remote" > /tmp/commands.txt

echo "put *.*" >> /tmp/commands.txt

echo "quit" >> /tmp/commands

expect -c "spawn sftp -o "BatchMode=no" -b "/tmp/commands.txt" "ftpuser@ftphost.revelationtech.com" expect -nocase \"*password:\" { send \"ftppassword\r\"; interact }"

* This script may now work from the crontab.

Private Key Authentication

cd /source_directory_on_local

echo "cd /target_directory_on_remote" > /tmp/commands.txt

echo "put *.*" >> /tmp/commands.txt

echo "quit" >> /tmp/commands

sftp -oIdentityFile=/home/oracle/.ssh/id_rsa -b /tmp/commands.txt ftpuser@ftphost.revelationtech.com