How to automate ftp login and file transfer

August 6, 2009
By Andrew Lin

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/

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
  • del.icio.us
  • IndiaGram
  • IndianPad

4 Responses to “ How to automate ftp login and file transfer ”

  1. Debt Settlement on October 20, 2009 at 3:38 am

    Sorry for my bad english. Nice post but I think you should add more to this topic as it looks 50% covered.

  2. loan on October 12, 2009 at 2:25 am

    cool nice site

  3. lilikindsli on October 4, 2009 at 3:59 pm

    SZBxEu I want to say – thank you for this!

  4. [...] ago I had posted a Korn script to automate FTP login and upload of file on a Unix or Linux computer. The default shell for most Linux servers is bash. I [...]

Leave a Reply