How to use ftp in combination with .netrc
After answering a number of questions regarding automatic FTP on
Unix-systems and the involvement of .netrc in it, I created this
little guide.
Permissions and location of the .netrc
The .netrc should be in your home-directory and the permissions on
the file should be unreadable for everybody except the owner:
[~] edwin@k7>ls -al .netrc
-rw------- 1 edwin wheel 246 Aug 27 16:14 .netrc
|
You can set to these settings with chmod 600 .netrc.
Layout of the .netrc
Machine definitions
The first part of the .netrc is filled up with host-definitions:
machine ftp.freebsd.org
login anonymous
password edwin@mavetju.org
machine myownmachine
login myusername
password mypassword
|
What it is saying now is nothing more than "If you connect to
ftp.freebsd.org, login as anonymous and use
edwin@mavetju.org as password." and "If you connect to
myownmachine, login as myusername and use mypassword
as password.".
Macro definitions
This part of the .netrc consists of macros which can be used to
perform automated tasks:
macdef uploadtest
cd /pub/tests
bin
put filename.tar.gz
quit
macdef dailyupload
cd /pub/tests
bin
put daily-$1.tar.gz
quit
|
Keep in mind that there should be an empty line after the last
macdef statement. If you don't do this, ftp will complain about it.
The first one is saying "Go to the /pub/tests directory, switch to
binary mode, put a file there and quit it". The second one is saying
the same, except that the name of the file is based on a parameter
on the macro-call (see Usage of the .netrc for more about
this).
Usage of the .netrc
Macros can be called from both inside ftp:
[~] edwin@k7>ftp myownmachine
ftp: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
220 myownmachine FTP server (Version 6.00LS) ready.
331 Password required for myusername.
230 User myusername logged in.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> $ uploadtest
cd temp
250 CWD command successful.
put filename.tar.gz
local: filename.tar.gz remote: filename.tar.gz
150 Opening BINARY mode data connection for 'filename.tar.gz'
100% |**************************************************| 1103 00:00 ETA
226 Transfer complete.
1103 bytes sent in 0.01 seconds (215.00 KB/s)
quit
221 Goodbye.
|
...or from on the command-line:
[~] edwin@k7>echo "\$ uploadtest" | ftp myownmachine
ftp: connect to address ::1: Connection refused
Trying 127.0.0.1...
100% |**************************************************| 1103 00:00 ETA
|
There is not much information here, because ftp doesn't expect a
terminal here. If you use ftp -v, there will be more output.
An example with arguments is
[~] edwin@k7>echo "\$ dailyupload `date +'%Y%m%d'`"
$ dailyupload 20010827
[~] edwin@k7>echo "\$ dailyupload `date +'%Y%m%d'`" | ftp myownmachine
ftp: connect to address ::1: Connection refused
Trying 127.0.0.1...
100% |**************************************************| 1103 00:00 ETA
|
It will upload the file daily-20010827.tar.gz.
Debugging of ftp-sessions
What can go wrong
The only problems I've encountered are
- People forgetting to add an empty line after the last macdef statement.
- People with incorrect permissions on the .netrc-file.
Debugging
- If you are running the macro via the commandline, use the -v
option to see more output.
- If you run ftp with the -d option you will get all commands
send to the ftpd.
|