McWalter.org :: Automating ftp


Despite its being a horrid, outdated, NAT-unfriendly, and deeply insecure protocol, FTP is still a fact of everyday life. The normal (BSD derived) command-line ftp client isn't immediately amenable to automation, but both it and some of its more modern command-line replacements can still be used successfully from scripts, given a little shell magic.


  ftp -i -n otherhost << EOF                         
    user myname mypassword                           
    mput foo bar                                     # put normal ftp comamnds here

  #   -i for "don't prompt before filetransfers"  
  #      -n for "don't ask for user/password"

  EOF                                               

An equivalent way (which has no real advantage over the above way, as far as I can tell) is available to sh and bash scripts:

  echo "
    user myname mypassword                           
    mput foo bar                                     # put normal ftp commands here
  " | ftp -i -n otherhost                            # -i for "don't prompt before filetransfers"  
                                                     # -n for "don't ask for user/password"

If you only need to download files from a remote machine using ftp, you can also use the GNU wget program. Wget is available for download from its homepage.

  wget ftp://myname:mypassword@otherhost/foo         # download "foo" from "otherhost"

Lastly, and perhaps the easiest solution, is to use the automation-friendly tools that are part of the ncftp suite. Chief (for our purposes) among these are ncftpput and ncftpget.

  ncftpput -u myname -p mypassword otherhost . foo   # upload files
  ncftpget -u myname -p mypassword otherhost . bar   # download files

You can get ncftp (it's free, and open source) from its homepage at http://www.ncftpd.com/ncftp