Wednesday, April 7, 2010

How to do non-interactive FTP in Unix?



   FTP is one of the means used to transfer files from one system to another, either way. FTPing files is one of the routine tasks for a developer or a system administrator.  There could be instances when an individual's task involve FTP to be done quite frequently.


  Every time when FTP is done, one needs to login to the destination system using the destination user  credentials, and do the file transfer. This is called interactive FTP. This process is pretty time consuming. Non-interactive FTP, as the name suggests, is a process in which the user has minimal interaction with the system to do FTP.  The user credentials and the files information are used in an non-interactive manner in this method.

   Let us consider a scenario: A file named 'alpha.c' needs to the put to the UNIX box 'TeakWood' under the user 'blogger'.

   The following steps describe the non-interactive FTP steps to do the file transfer:

   1. Create a file named .netrc in the home directory.

#touch .netrc

   2. Add the following contents in the .netrc file:

       Syntax:

machine <hostname> login <username> password <password>
macdef init
prompt
put <filename>
bye


          where 'hostname' is the hostname of the destination machine
                    'username' is the user name of the destination machine
                    'password' is the password of the destination user.

       Actual:

machine TeakWood login blogger password abcd123
macdef init
prompt
put  alpha.c
bye


          The file .netrc is saved in the home directory with the above contents .

   3. Change the permission of the file to read-only to the user and none to the group and others.


#chmod 400 .netrc

    4.Execute the following command from the command prompt to do FTP:

#echo “quit” | ftp –v TeakWood

      The file got FTP'ed successfully to the destination box TeakWood using non-interactive FTP.  In our future articles, we will see how to write a script to automate the non-interactive FTP.
Related Posts Plugin for WordPress, Blogger...

2 comments:

  1. Nice one,.,, Can you please explain the meaning of
    "macdef init"

    Regards
    Gaurav

    www.Udzial.blogspot.in
    udzial means Share

    ReplyDelete