Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send password using sftp batch file

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.

like image 877
judith Avatar asked Jul 31 '12 10:07

judith


People also ask

How do I pass a username and password in SFTP?

Create a file put-script : open sftp://user:password@host; put local-file.name; exit Than run lftp -f put-script This way you do not have to have the username and password in a command line and can set up restrictive permissions to your script file.

How do I add a password to a batch file to automatically authenticate?

To automatically authenticate, add the /savecred flag, enter the password on the script's first run, once you execute, it will be saved after that in Credential Manager.

What is batch mode in SFTP?

You can use "Batch Mode" of sftp. From manual: > -b batchfile > Batch mode reads a series of commands from an input batchfile instead of stdin. Since it lacks user interaction it > should be used in conjunction with non-interactive authentication. A batchfile of '-' may be used to indicate > standard input.


2 Answers

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.

like image 113
Joe Van Dyk Avatar answered Oct 11 '22 18:10

Joe Van Dyk


If you are generating a heap of commands to be run, then call that script from a terminal, you can try the following.

sftp login@host < /path/to/command/list 

You will then be asked to enter your password (as per normal) however all the commands in the script run after that.

This is clearly not a completely automated option that can be used in a cron job, but it can be used from a terminal.

like image 34
Tigger Avatar answered Oct 11 '22 19:10

Tigger