How to automate FTP-ing file to another FTP account?

We will be using shell script to automate the transfer between two FTP accounts. You’ll need shell access to the source FTP account.

#!/bin/bash
directory="/home/raisya/public_html"
filename="test.txt"
hostname="ftp.destination.com"
username="dest"
password="qwerty"
logfile="ftpf.log"
ftp -uni $hostname <<_ftp>>$logfile
quote USER $username
quote PASS $password
lcd $directory
put $filename
quit
EOF

Explanation:

directory="/home/raisya/public_html"

Assigning “/home/raisya/public_html” to directory.

filename="test.txt"

Assigning “test.txt” to filename.

hostname="ftp.destination.com"

Assigning “ftp.destination.com” to hostname.

username="dest"

Assigning “dest” to username.

password="qwerty"

Assigning “qwerty” to password.

logfile="ftpf.log"

Assigning “ftpf.log” to logfile.

ftp -uni $hostname <<_ftp>>$logfile

Initiate FTP connection to destination FTP account, with -uni switch and log the FTP connection to $logfile.

Switch:

u: Restrains ftp from attempting auto-authentication upon initial connection.
n: Restrains ftp from attempting ââauto-loginââ upon initial connection.
i: Turns off interactive prompting during multiple file transfers. This is optional here, since we only sending one file.

quote USER $username

Send FTP USER command.

quote PASS $password

Send FTP PASS command.

lcd $directory

Change local directory to $directory.

put $filename

Send $filename to destination FTP account.

quit

Stop FTP connection.

EOF

End Of File.

chmod the file to enable execution. Ie:

chmod 755 ftpscript.sh

To fully automate the process, you can set up a cron job for this. To run this every minutes:

* * * * * /home/raisya/ftpscript.sh

Tagged with 
About sepedatua
I am nothing special, of this I am sure. I am a common man with common thoughts and I’ve led a common life. There are no monuments dedicated to me and my name will soon be forgotten, but I’ve loved another with all my heart and soul, and to me, this has always been enough.

Leave a Reply