Scripting with lftp in ACTIVE mode
I needed to automate an FTP file transfer today. Normally, I’d just use a short bash script to tell ftp what to do; but since the server I was uploading to only accepted ACTIVE FTP connections (a windows box maybe?), the plain-vanilla ftp wouldn’t work.
After a little research, I discovered lftp. To enable ACTIVE mode for lftp in CentOS 4, edit /etc/lftp.conf. I uncommented two lines: 33 (set ftp:passive-mode off) and 18 (set default-protocol/ftp.* ftp). Once you save /etc/lftp.conf and exit your text editing program, you’re ready to start writing the actual bash script that will make everything happen.
First, in a non-publicly accessible folder (we’re going to be hardcoding the FTP password within the script), create a new, blank file, named however you want:
touch lftp.sh
Then, be sure to chmod +x the file, so the OS knows that it’s executable.
This is what I put in my lftp.sh file:
lftp -u username,password hostname <<EOF
cd yourdirectory
mput /dir/to/your/file(s)/*.csv
quit 0
EOF
My lftp.sh file transfers all .csv files in the folder specified to the remote FTP server every time lftp.sh is run. Make sure to have the two left brackets and the EOF right after your lftp connecting information:
<<EOF
is the functional equivalent of hitting “Enter” on your keyboard. It allows the rest of the commands to be executed once connected.
Then, I set up a cronjob to run lftp.sh every night at five minutes after midnight. Voila! Instant lftp file transfer goodness, even with ACTIVE mode on.
Posted on Oct 06, 2008 - 06:05 PM
