FTP a file from one server to another, automatically
We are using an ASP script to create iCal files from a database, so another department can display the events on their web-based calendar.
Their calendar page is outside the corporate firewall. The ASP script needs to reside on and IIS server within the firewall. I needed a fool-proof way to transfer the ical file nightly out to an external web server so it could be accessed by any calendar applications that need to use it. The best way I’ve found so far is to create a batch file with command-line FTP instructions, and add it to a Windows scheduled task.
- To do this with your own files, create a directory that will hold the batch file somewhere on the server. Since the batch file will contain your destination server’s ftp password in plaintext, you will want to secure the directory. Create a Windows or domain account whose password will never change. I happened to have a "utility" domain-based account that already had access to some parts of the server, so I gave that account rights to the new directory.
- Create a new text file inside the directory called "fileup.bat"
. Enter the following text. The items in red need to be customized: add your ftp username and password, and the file-system path to the destination directory on the receiving server. Save the file. Test it by running it in the command window.
echo user ftpusername> ftpcmd.dat
echo ftpuserpassword>> ftpcmd.dat
echo cd /pathto/destination/directory/>>ftpcmd.dat
echo bin>> ftpcmd.dat
echo put %1>> ftpcmd.dat
echo quit>> ftpcmd.dat
ftp -n -s:ftpcmd.dat ftp.umhscompliance.net
- Test the file by running it in the command window:
Connected to external.server.com.
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 2 of 50 allowed.
220-Local time is now 12:40. Server port: 21.
220 You will be disconnected after 15 minutes of inactivity.
ftp> user ftpusername
331 User ftpusername OK. Password required
230-User ftpusername has group access to: ftpusername
230 OK. Current restricted directory is /
ftp> cd /pathto/destination/directory/
250 OK. Current directory is /pathto/destination/directory/
200 TYPE is now 8-bit binary
ftp> put E:contentcbtlibmodulestestrssicalDemo.ics
200 PORT command successful
150 Connecting to port 4297
226-File successfully transferred
226 0.174 seconds (measured here), 331.30 Kbytes per second
ftp: 59172 bytes sent in 0.00Seconds 59172000.00Kbytes/sec.
221-Goodbye. You uploaded 58 and downloaded 0 kbytes.
221 Logout. E:fileStorageftp>
Log in to the destination webserver and check that the file has arrived, in the right directory.
- Create a scheduled task for this procedure. Select Add Scheduled Task as shown.
- Click Browse to find the batch file you want to schedule.
- Select the file and click Open.
- Type a name for the task, and how often to perform the task. Click Next.
- Select a time and start date, and click Next.
- Enter the name and password of the Windows "utility" user mentioned earlier. The password should never change, or the task will stop running.
- Check "Open advanced properties for this task…" and click Finish.
- In the Run box, type a space after the name of your batch file, and add the filesystem path to the file you want to send to the other server. In the example shown here, the full entry reads: