How to send/get a file to/from a remote server via command line or script
A user might need the ability of automatic downloading or uploading a file to a remote machine via a secure protocol. Several methods can be used and using LFTP seems to be the simplest (however a bit unsafe). The second method is exchanging the authentication key and using SCP.
Contents |
Via LFTP
Install LFTP
slapt-get -u slapt-get -i lftp
LFTP Usage
LFTP can be used as any typical FTP client but it also provides the ability to connect without asking about the password. The solution is simple however a bit unsafe as the password is given explicitly as text, like below. Thus anybody who can read the script can also steal the password, so make sure your script is readable for you only. Also, do not connect as root. Create a special user for this without root super-powers. The example below shows how to get a file from a remote server via secure SFTP protocol (bye at the end closes connection):
lftp sftp://user:password@host -e "get remote-file.name; bye"
In case the port for SSH connection is changed, then try:
lftp sftp://user:password@host:port -e "get file.name; bye"
To send a file to a remote server:
lftp sftp://user:password@host -e "put local-file.name; bye"
Via SCP
First you have to generate and copy the public ssh key to the remote host like described here How to login via ssh without password
When you are able to login without a password you can use SCP (secure copy) tool like this:
in order to download a file
scp user@remote.host:filename local.filename
in order to upload a file
scp local.filename user@remote.host:remote.filename