RSS

ftp scripting

22 Apr

Windows ships with the old as dirt “ftp.exe” which is an FTP client. It is a command prompt only program that you can use to transfer files to a computer running an FTP server. It has scripting functionality but is severely lacking when compared to other programs in its class. Some FTP programs I have used are:

ftp.exe – blah, no GUI, no TLS
FileZilla – awesome and open source but no CLI for scripting, also has server capabilities
WS_FTP – Ipswitch’s version which has been around for ages. Client and server, can be scripted and has retry logic available. Free and professional editions.
coreftp – GUI, not sure if you can script this one but it is free
WinSCP – Another freebie with GUI and CLI. Verbose in its logging which can help diagnose connection/firewall issues.

http://support.microsoft.com/?scid=kb;EN-US;812409 even has a downloadable plugin to .NET code so you can write your own. Or, here is a VB.NET WinSCP example http://winscp.net/eng/docs/script_vbnet_robust_example

The GUI concept is pretty simple, connect then your computer is on the left and the remote computer is on the right. Drag, drop and your done.

The first step would setup a connection profile. This you can do in the GUI of everything except ftp.exe I listed above. You store the server information in the connection profile. This includes minimally the Hostname and Username and a name to reference the connection profile by. More information can be stored in this profile such as, port, password, starting directory, number of retries, connection type (ftp, ftps, sftp) server and/or client keys and much more.

WS_FTP pro supports re-tries, timeouts, keepalives, key generation/storage, and more features than your average FTP program but there isn’t much sense to pay for that unless you are running a huge FTP operation.

Scripting is fairly simple. You really only need two files, a batch file (.bat) which you line up some commands and a script file that you pass into the FTP client program to do the actual transfer.

ftp.scp:

USER username pwd
PUT test.txt
BYE

ftpfiles.bat:

c:
cd scriptsftp
ftp -i -s:ftp.scp 10.10.10.11
pause

The ftp protocol uses port 21 by default and sends passwords in plain text to the server. Windows ftp.exe can only use this protocol. Both of these should be avoided if there is any spec of data that should be secured. The SSH protocol can transfer files securely using keys to encrypt the transport layer. This is refered to as SFTP and any of the clients besides ftp.exe can use the protocol. FTPS also uses keys to encrypt the transport layer. It comes in implicit and explicit types. WS_FTP pro can use this method and WinSCP cannot.

I would recommend WinSCP and SFTP. Eariler I posted instructions on setting up my server which include the easy steps to configure an SFTP server. If you use WinSCP you can catch errors in your batch file by checking the %errorlevel% on the line after you call winscp.exe. Standard ftp.exe returns 0 even if it cannot connect to the ftp server. If there are no files to download on the remote server WinSCP will return 0 and WS_FTP will return 1. If you are expecting files every time your script runs you should check “IF EXISTS” after checking the %errorlevel%.

Download and install WinSCP. Next you would setup a connection profile. After adding the winscp install directory to your PATH environment variable you can change your .bat file to:

c:
cd scriptsftp
winscp.com /script=example.scp
pause

To upload a single file, example.scp would look like this:

option batch abort
option confirm off
open user@example.com
put examplefile.txt /home/user/
exit

The password and encryption keys are stored in the connection profile “user@example.com”. There is a winscp.ini file that contains this information that shows up when you launch the GUI. The passwords are encrypted but I recommended guarding this .ini file because the encryption can be cracked. WinSCP does not recommended you even save the passwords for your connection profiles.

Advertisements
 
Leave a comment

Posted by on April 22, 2011 in Network Admin

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: