How to send password using sftp batch file

judith picture judith · Jul 31, 2012 · Viewed 110.3k times · Source

I'm trying to download a file from sftp site using batch script. I'm getting the following error:

Permission denied (publickey,password,keyboard-interactive).
Couldn't read packet: Connection reset by peer

When running the command:

sftp -b /home/batchfile.sftp <user>@<server ip>:<folder>

the batchfile.sftp includes these data:

password
lcd [local folder]
cd [sftp server folder]
get *
bye

Note: It's working when running at the prompt as

sftp <user>@<server ip>:<folder>

But I need the ability to enter the password automatically.

Answer

Joe Van Dyk picture Joe Van Dyk · Feb 1, 2014

You'll want to install the sshpass program. Then:

sshpass -p YOUR_PASSWORD sftp -oBatchMode=no -b YOUR_COMMAND_FILE_PATH USER@HOST

Obviously, it's better to setup public key authentication. Only use this if that's impossible to do, for whatever reason.