As a Linux/Unix server administrator I ran into an occasion where I had to automate the file transfer via ftp to a remote server. I had to create a shell script to automatically login to the remote ftp server and then upload the file. I then schedule this script to run daily through a cron job.
Here is the script to automate the ftp login process and upload the file. Save the file as autoftp.
#! /usr/bin/ksh
# The above line is required to define the file as a Korn shell script
# go to directory where the file you want to upload iscd /directory/containing/file/to/upload
# Define the variables
HOST=remote.ftp.server.name
USERID=andrew_lin
PASSWORD=can’t_tell_u
exec 4>&1
ftp -nv >&4 2>&4 |&
print -p open $HOST
print -p user $USERID $PASSWORD
print -p binary
# Upload the file myfile.txt
print -p put myfile.txt
print -p bye
To schedule the autoftp script to run through a cron job, create a file called ftp_cron. The below is the contents of ftp_cron.
MAILTO=andrew_lin@andrew-lin.com
22 15 * * * ksh /mydir/autoftp
The MAILTO command will email the logs to andrew_lin@andrew_lin.com. If you wanted to email more then one recipient then separate multiple email addresses with a comma, e.g. MAILTO=andrew@andrew.com,andrew_lin@andrew_lin.com. The job is schedule to run daily at 3:22 p.m. (15:22). Preceding the script with ksh means that the script will execute in a separate Kron shell.
Now issue the below command to schedule the corn job.
crontab ftp_cron
To confirm if the jobs has been scheduled enter the below command.
crontab -l
If you want to make and changes to the schedule simply edit the ftp_cron file and run crontab ftp_cron again.
Here is link to the bash version of the script, http://www.gamescheat.ca/2009/08/27/how-to-automate-ftp-login-and-file-transfer-with-bash-script/