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.
Expect is a great program to use.
On Ubuntu install it with:
On a CentOS Machine install it with:
Lets say you want to make a connection to a sftp server and then upload a local file from your local machine to the remote sftp server
This opens a sftp connection with your password to the server.
Then it goes to the directory where you want to upload your file, in this case "logdirectory"
This uploads a log file from the local directory found at /var/log/ with the files name being file.log to the "logdirectory" on the remote server
Another way would be to use lftp:
The disadvantage of this method is that other users on the computer can read the password from tools like
ps
and that the password can become part of your shell history.A more secure alternative which is available since LFTP 4.5.0 is setting the
LFTP_PASSWORD
environment variable and executing lftp with--env-password
. Here's a full example:LFTP also includes a cool mirroring feature (can include delete after confirmed transfer '--Remove-source-files'):
I was recently asked to switch over from ftp to sftp, in order to secure the file transmission between servers. We are using Tectia SSH package, which has an option
--password
to pass the password on the command line.example :
sftp --password="password" "userid"@"servername"
Batch example :
I thought I should share this information, since I was looking at various websites, before running the help command (
sftp -h
), and was i surprised to see the password option.For searchers that don't care that the password can be seen in the command-line command:
sftp userid:password@remoteHost
is how to include the password in thesftp
connect command.You can override by enabling Password less authentication. But you should install keys (pub, priv) before going for that.
Execute the following commands at local server.
Press ENTER for all options prompted. No values need to be typed.
Connect to remote server using the following command
Execute the following commands at remote server
Execute the following command at local server to test password-less authentication. It should be connected without password.
Bash program to wait for sftp to ask for a password then send it along:
You may need to install expect, change the wording of 'Password' to lowercase 'p' to match what your prompt receives. The problems here is that it exposes your password in plain text in the file as well as in the command history. Which nearly defeats the purpose of having a password in the first place.