net81813


.netrc for automating ftp client operation

Posted in Uncategorized by net81813 on the 6, February 2006

I have for to automate ftp uploading and ftp downloading with .netrc file. In this file we can save username and password to connect to ftp.

As this reside in user’s home directory so no need to worry about security. Just change file permission to -rw——- (600) so it can only read and write by owner.

To save username and password in following format.(following 3 lines)

machine ftp.ftpserver.com
login username
password mycodeword

In above 3 lines machine, login and password are file reserve words.
machine for specifying address of ftp server.
login for specifying username of ftp server.
password for specifying password of ftp server for above username.

In .netrc file you can write multiple entries. But for that just keep a 1 blank line between 2 entries.

Creating macro in .netrc
Macro is another very good feature. Using it many tasks can be automated. In sort we can say this a name give to group of ftp command. So for perticular task you have to write 6-10 commands to performe that task and you are doing that task repeatatively then you write macro.
You can write macro in following way.

macdef macro1
cd /d01/abc/dir1
put abc.txt
put abc.tar.gz
get def.pdf
quit

Here macdef is reserve word for .netrc file for rest of lines are  ftp commands with arguments.
macdef is used to define macro so in this above example macro1 is name of newly defined macro. macro1 can be used to execute commad set of command followed by macdef line.

macro can use parameter also
macdef macro2
cd /d01/abc/dir1
put $1.tar.gz
quit

When above macro is called it will take argumen if supplied and 1st argument will be used in script in above example see line put $1.tar.gz $1 is replaced by supplied argument. (See how to call macro with argument below.)

In one .netrc file there can be more than one macro. Rule is same as above that keep a blank line after each macro. (dont forgot to put a blank line after last macro).

Using macro

First way is start normal ftp session from command line. use macro name as command from ftp> prompt.

ftp>macro1
Above will execute ftp command defined under macro1.
You can call in following way also.

root@localhost$ echo  “\$ macro1″ | ftp ftp.ftpserver.com

as

root@localhost$ echo “\$ macro2 `date +’%Y%m%d’`” | ftp ftp.ftpserver.com

This will receive a parameater and performe action useing that parameter.

Suggestions:
* Set .netrc file permission to 600 (-rw——-)
* Add a blank line after each macro.
* use -v option with ftp to get more information.

Leave a Reply