Walker News

Auto File Transfer Via FTP Batch Scripts In Both Windows And Linux

There are many ways to automate file transfer between networked computer. In the good old days, we rely on the simple-to-use ftp client that implement ftp protocol (port 21) for file transfer.

Even now, there are still number of implementation here and there, that automate file transfer between networked computer via ftp protocol.
First, we target the ftp client of Windows platform – How to automate file transfer via ftp client in Windows XP / Windows Vista?
  1. Create ftp command template file with these sample command and give it a meaningful file name, e.g. walker-ftp.tpl
    open ftp.walkernews.net
    user walker-login-id-here
    walker-login-password-here
    prompt
    mget *.*
    by

    Understand the ftp command template:
    • use the mandatory keyword open to specify the target computer that the ftp client connect (open) to, e.g. ftp.walkernews.net
       
    • use the mandatory keyword user to specify the user ID to login the ftp server
       
    • specify the password used to authenticate the user login ID specify in step (b)
       
    • assuming login to ftp server is successful, this step onwards are the normal ftp client compatible commands.
      For example, type ftp at Command Prompt and type help at the ftp prompt will display all the ftp client compatible commands. To get further help description of each ftp client command, type help command, e.g. help prompt

      The prompt keyword is use to turn on (default) or turn off interactive prompting on multiple commands. For example, if you don’t want to answer Y or N when execute mput or mget command. Essentially, it’s identical to explicitly specify -i option switch to the ftp client.

      You can type ftp -? at Windows Command Prompt for more ftp client option switches.
    • The mget *.* ftp command is to get / download all files in the target directory on remote ftp server to the working directory of the local computer.
       
    • at last, execute by or bye command to end or terminate current ftp connection. 
  2. Now, you can type this single command to instruct Windows ftp client program to automate file transfer /downloading with all the ftp commands specify in the template.
    ftp -n -s:walker-ftp.tpl

    You can either run it at Command Prompt window, or create a ftp batch scripts (Windows batch file) for the command – just double-click the ftp batch file to execute!


There are equivalent implementation in Linux / Unix too, but the syntax is quite different. So, let’s see how to create a Linux scripts to automate file transfer with FTP client in Redhat Enterprise4.
  1. In the user home directory, create a hidden file called .netrc that contains these auto-ftp commands
    machine 10.1.1.10
    login walker-login-id-here
    password walker-login-password-here
    macdef init
    prompt
    mget *.*
    quit
    Leave one mandatory blank line here
    Leave one mandatory blank line here
    
    machine ftp.walkernews.net
    login walker-login-id-here
    password walker-login-password-here
    macdef init
    pwd
    quit
    Leave one mandatory blank line here
    Leave one mandatory blank line here

    Understand the .netrc file content
    • the keyword machine is used to specify the target ftp host that the ftp client connect to
       
    • the keyword login is used to specify the user login ID
       
    • the keyword password is used to specify the password used to authenticate the user login ID
       
    • the keyword macdef init is used to define the auto-ftp macro command that named as init (refer to man netrc for more information)
       
    • supposed the login authentication is successful, then the rest of lines after macdec init down to quit keyword are ftp client commands. The quit is identical to the bye command.
       
    • after the quit keyword, press ENTER twice to leave two empty lines! These two lines are mandatory syntax of .netrc file for auto-ftp. As of this .netrc sample file, I’ve another ftp server added right after the first two empty lines, and end the second ftp server with another two empty lines as well. 
  2. Execute chmod 600 .netrc to limit only read and write permission for the login user ownership only. This is a must in order ftp client to automate file transfer!
     
  3. Now, if you want to auto-ftp with ftp host 10.1.1.10, just type this command
    ftp 10.1.1.10

    and the Linux ftp client will automatically execute the series of ftp commands in .netrc file for the matching ftp host.

    Similarly, you can type ftp ftp.walkernews.net for auto-ftp file transfer with the second ftp host.


Custom Search

  1. Auto SSH Login By Using Public-key Cryptography – Walker News 04-06-08@12:10

    […] without encryption feature. Thus, password-less SSH login is extremely useful in writing secured shell scripts that need to automatically (non-interactively) login remote server and then initiates file transfer […]

  2. How To Setup Non-Interactive SSH Login – Walker News 06-06-08@15:52

    […] password for authentication. But, how could I handle this interactive authentication process in Windows batch files or Linux shell scripts that scheduled to trigger file transfer between networked hosts on a daily […]

  3. sanjay sachdeva 21-06-08@12:00

    very usefull tips. very knowledge full. please send me tips via mail. thanks

  4. das 15-12-10@13:55

    hey i have problem regarding

    1. creating batch file to connect windows machine,

    how to run ftp in the background of windows, if i want to connect two windows machine, and through ftp

  5. Walker 15-12-10@23:56

    Maybe run the batch file by Windows Task Manager (scheduler).

2014  •  Privacy Policy