Windows Scripted FTP
This page covers an example of how to use the command line FTP client included with most versions of Microsoft Windows to retrieve log files from an IPAD web server without any user intervention. This can then be automated on a schedule with the AT command or any number of third party scheduling tools.
There are two requirements for this script example to work:
- 1. The script runs in a command line window on a Windows machine and the settings for that window should not prevent DOS programs from detecting Windows.
- 2. The command window session must have access to the FTP.EXE program. This is available by default on a Windows system through the DOS path. You can test for this by opening a DOS window and, on the command line, type
FTPand press Enter. If the prompt changes to
ftp>then the program is available. Type
byeto exit. If a
bad command or filenameerror displays, check your Windows help files and manual for the correct settings to enable command line FTP. Also check the properties for the DOS window to make sure that the
Prevent DOS programs from detecting Windowsbox is not checked because this prevents the command line FTP program from using the Windows TCP/IP networking connection.
There are two files that make this scripted connection work. They are FTPLINK.BAT and SCRIPT.TXT.
The FTPLINK.BAT is a batch file that may be run from the command prompt window under any version of Windows since Windows For Workgroups (3.11). The batch file shown below requires a command line parameter of the domain name or IP address of the IPAD FTP server. An example is shown to you if you type
FTPLINK (no parameters) on the command line and press the Enter key.
The SCRIPT.TXT file is nothing more than an ASCII text file with the commands listed one per line that are to be sent to the FTP server's prompt when available.
The first two lines of the SCRIPT.TXT file are special. The first line is the name of the user ID used for the FTPLINK FTP connection. This should be a special account that you set up on the IPAD that gives access to the log files you wish to retrieve. This account needs to have READ, WRITE and DELETE permission or things won't work as expected.
The second line of the SCRIPT.TXT file is the case sensitive password for the account described above. Make sure it is a password that is not easy to guess. This account, like the supervisor account, has access to very sensitive log information on your system.
The following example files use
file renaming to ensure the smallest amount of time where a file will not be available to the IPAD as well as to prevent changes to files while the transfer is happening.
An example of the user account in your FTP.USR authorization file is:
- userid password c:\ipad\control READ WRITE CREATE DELETE
An example of the FTPLINK.BAT is:
:: (The "%1" parameter holds the domain name or IP number of the IPAD.)
if "%1" == "" goto error1
:: ***** Delete temp filenames.
if exist billing.old del billing.old
:: ***** Call Windows FTP client with script.
:: (Use two greater than signs if you wish to make it log all sessions.)
ftp -i -s:script.txt %1 > session.txt
:: (Note - SESSION.TXT will have a text copy of the latest FTP session.)
:: ***** If BILLING.LOG is here then append to it.
if exist billing.log ren billing.log billing.tmp
copy billing.tmp + billing.old billing.new
:: ***** Check to see if billing.new was created.
if not exist billing.new goto error2
:: ***** Put BILLING.LOG back into place.
ren billing.new billing.log
:: ***** Clean up old parts of LOG after it is appended.
:: ***** Fix stranded billing log if it is here.
if exist billing.tmp ren billing.tmp billing.log
:: ***** Error display for billing log creation or append problem.
echo ERROR: The new BILLING.LOG file (BILLING.NEW) could not be created!
echo This can be caused by a lack of available drive space on this disk
echo or an inability to read the BILLING.TMP or BILLING.OLD files due to
echo some other application locking the file access.
:: ***** Error display for not providing a domain name or IP address on
:: ***** the command line.
echo ERROR: The domain name or IP address of the IPAD-OS was not given on the
echo : command line.
echo Syntax: (one of the following domain name or IP address)
echo : FTPLINK ftp.example.com
echo : FTPLINK 192.168.1.1
An example of the SCRIPT.TXT is:
(Note: The lines starting with a double colon are comments and should be removed from your file.)
:: The FTP account password
:: Force binary transfers
:: Erase old hold file (ignored if it does not exist)
:: Rename file so it won't change before we download the whole thing
rename billing.log billing.old
:: Download the renamed file
:: End the FTP session
The examples above show how you can use the same script to download log files from more than one IPAD-OS server with the same script. This is possible because the calling batch file accepts the domain name or IP address as a command line parameter and adds that to the FTP program's command line. But what if you wanted the script to hold that information? This is possible by adding a single line to the top of the script file.
The open command tells the FTP program to connect to a server. It can use a domain name or IP address just like the FTP program command line. But this option gives the script control of the server and coupled with the
CLOSE command gives a single script the ability to connect to more than one FTP server in sequence.
With a little imagination, it would be possible to copy files from one FTP server to another all with a single script. Feel free to play.First published 2007-06-10. The last major review or update of this information was on 2010-08-07. Your feedback using the form below helps us correct errors and omissions on this page.