Problem solve Get help with specific problems with your technologies, process and projects.

Scripting FTP using the .netrc file

Calling FTP from a script can be difficult; a quick and dirty way to do this is using the .netrc file.

A very common stumbling block people run into occurs when they try to have a script (or cron job) perform an ftp.

It would seem that something like:

ftp somehost <<EOF
put somefile

Would work, but due to stream handling on the part of ftp, it does not read from the document (or even an input redirected file), so you get prompted for the password, and the non-interactive session does not occur.

A fairly complex, but very powerful solution would be to learn the Expect language, and write some code in Perl or TCL, or some language that can include Expect. This is a good solution for dynamic situations.

If you want a quick and dirty (and less powerful) solution try using the .netrc file. IT is a reasonable thing to do when high security is not an issue (such as ftp's within a network, or a daily anonymous ftp get).

Here is the flow:

When ftp is invoked without the -n option, an autologin is attempted. Autologin occurs if:

  • There is a .netrc file in the users home directory.
  • That file is owned by the user running ftp
  • The file has rights of 00 for group and others, with read rights for the owner (such as 600).
  • There is a "machine" entry that matches the server argument on the command line.

For example, if you executed:

ftp host1

And you had a .netrc file with the following:

machine host1 login myaccount password mypassword

You would be automatically logged into host1. Normal prompting would occur for any other hostname (or IP address). Another allowed keyword is default, which you could use for various unlisted sites that allow anonymous logins.

Putting this together into the scripting scenario mentioned above, the following would now work in a shell script:

ftp host1 <<EOF
get somefile

If you wanted to do an mget or mput, you could either use the -i option to ftp, or include the "prompt off" ftp command as seen in the Perl snippet below:

  open(FTP, "|ftp host1>/dev/null");
  print(FTP "prompt offn");
  print(FTP "cd somedirn");
  print(FTP "put somefilen");
  print(FTP "byen");
  if ($?==0){
    #do some success thing
  } else {
    #do some unsuccessful thing (log or email?)

Fred Mallett is founder of FAME Computer Education, which provides standup delivery of educational classes on a variety of UNIX and Win32 related subjects.

This was last published in January 2006

Dig Deeper on WAN optimization and performance