Advanced Networking..
Automated FTP
Automate and schedule file transfers using command-line FTP
Advanced Adv Networking


Dateline January 2007 | Contributed by Andrew Mallett

Scenario

The plan is to upload files to a server on a regular basis, without user intervention. This could be part of a typical backup strategy.

Prerequisites

  • The client (the system uploading the files) and the server (the system which will be receiving the files) must be able to "see" each other on the network, i.e. be pingable


  • It is necessary to be completely familiar with using Command Line FTP. Ensure that files can be uploaded using FTP manually, before attempting any automation


  • The user also needs to be familiar with the Windows Task Scheduler, also known as the AT Command, if regular scheduled file transfers are anticipated.

1. Modifying the standard FTP command

The usual command-line FTP structure is short and sweet..

ftp  hostname

Command line FTP can be configured to use parameters from a text file, by using the '-s:' switch..

ftp  -s:full-path-to-text-file  hostname

So in the following example, the FTP command is invoked, instructed to use the text file located at d:\progs\ftp2.txt and is pointed towards a host named goth.it.net. You can also substitute that host name for an IP Address..

ftp  -s:d:\progs\ftp2.txt  goth.it.net

Note that the -s: switch must be followed by the full path to the text file which contains the FTP instructions. No spaces are allowed anywhere within the -s: parameter.

2. Creating the FTP Commands text file

The text file is a bit like a batch file in that it contains all the commands that you wish to run, starting with the FTP username and password. Note that there must be no empty lines or remarks at the top of this file as FTP will parse the file starting at line #1, on which it expects to find the ftp username.

The file itself can be located anywhere in the file system, as it will be referenced using its full path in the '-s:' switch as mentioned above. I like to use d:\progs for my custom scripts, as it's a short path name, located on a different partition to the Windows operating system files.

Here's a 10-line text file containing some example commands..

---------------------------------
myftpusername
myftppassword
bin
prompt
lcd  d:\somewhere
cd  /usr/local/apache/htdocs
mput *.exe
ascii
mput *.txt
bye
---------------------------------

As previously mentioned, the very top line contains my FTP username, followed by my FTP password on line #2. Note that by default, FTP sends these details across the network unencrypted. Additionally if somebody gets hold of this text file they will be able to see these details.

Line #3 puts the session into Binary mode for transferring non text files, such as executables and archives. Line #4 turns prompting off (are you sure, y/n?) for multiple file transfers using mput or mget. We don't want the whole thing to grind to a halt waiting for a user response do we..

The lcd command changes the local directory on the client, while the cd command changes the remote directory on the server.

mput on line #7 is the command for uploading multiple files and yes, FTP understands the use of wildcards such as *.exe for all files ending with the .exe extension.

The ascii command changes back to (default) ASCII mode for transferring text files and again all files in the current dir ending in .txt are uploaded.

The session is ended with the FTP bye command, which should always be on the last line (you could add exit to the very last line to close the command prompt window, although I have found this is not usually necessary).

You can have as many FTP text files as you like, to perform different types of file transfer. It is recommended you test and refine the FTP text file by running it manually and noting the results, before attempting to schedule it as an unattended job.

3. Scheduling a file transfer

Setting up an unattended file transfer is simply a matter of telling the Windows Task Scheduler when to run it. You could type the full FTP command into the AT command thus..

C:\>at 23:50 /every:m,t,w,th,f ftp -s:d:\progs\ftp3.txt myserver
Added a new job with job ID = 1

C:\>at
Status ID  Day              Time       Command Line
------------------------------------------------------------------------
        1  Each M T W Th F  11:50 PM   ftp -s:d:\progs\ftp3.txt myserver

However there's a fair bit to type into the AT command and it's easy to mkae typping mitsakes and have to delete the job and start all over again. I prefer to write a batch file to run each FTP job, which I name after and keep with, its accompanying text file. The batch file is then referenced from the AT command..

C:\>at 00:20 /every:m,t,w,th,f,s,su d:\progs\logbak.bat
Added a new job with job ID = 1

C:\>at
Status ID  Day                   Time       Command Line
---------------------------------------------------------------
        1  Each M T W Th F S Su  12:20 AM   d:\progs\logbak.bat

So in the last example above, I am telling AT to run the batch file logbak.bat, which in turn will use logbak.txt for its instructions..

d:\progs\logbak.bat:

@ECHO OFF
FTP  -S:D:\PROGS\LOGBAK.TXT  192.168.0.100

d:\progs\logbak.txt:

---------------------------------
andymallett
banana2007
prompt
lcd  c:\windows\system\logs
cd  /usr/local/andy
mput *.log
bye
---------------------------------

4. Set Permissions

Finally, make sure that any scheduled .bat and .txt files are readable and executable by the SYSTEM user, as this is the user that the AT command uses to run scheduled tasks. This can be achieved at the command prompt by running the following commands..

cacls  f:\progs\*.bat  /E  /G  system:R  administrators:F

cacls  f:\progs\*.txt  /E  /G  system:R  administrators:F

In the above example I have given the user SYSTEM Read (which includes Execute) permissions to all batch and text files in the F:\PROGS directory. I have also made sure that ADMINISTRATORS have Full Control over these files, as I may need to modify them myself. Substitute your own file and directory names as required. Again note the wildcard * is acceptable in the CACLS command.

- A.


Put 21.01.2007 | | Refresh Page |