I need to transfer a log file to a remote host using sftp from a Linux host. I have been provided credentials for the same from my operations group. However, since I don't have control over other host, I cannot generate and share RSA keys with the other host.
So is there a way to run the sftp
command (with the username/password provided) from inside the Bash script through a cron job?
I found a similar Stack Overflow question, Specify password to sftp in a Bash script, but there was no satisfactory answer to my problem.
You have a few options other than using public key authentication:
If you decide to give sshpass a chance here is a working script snippet to do so:
You can use sshpass for it. Below are the steps
sudo apt-get install sshpass
sshpass -p '#Password_For_remote_machine' scp /home/ubuntu/latest_build/abc.war #user@#RemoteIP:/var/lib/tomcat7/webapps
Combine sshpass with a locked-down credentials file and, in practice, it's as secure as anything - if you've got root on the box to read the credentials file, all bets are off anyway.
You can use lftp interactively in a shell script so the password not saved in .bash_history or similar by doing the following:
Add the following to your file:
And write/quit the vi editor after you edit the host, user, pass, and directory for your put file typing
:wq
.Then make your script executablechmod +x test_script.sh
and execute it./test_script.sh
.