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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With