Monday, September 18, 2006

FTP: Automated sessions via script

So you know how to use command line ftp clients, but you're not sure how to automate them with scripts.

WINDOWS (command-line)

ftp -s:script1.txt 11.11.11.11

script1.txt
username
password
ftp command1
ftp command2
quit

where username is the ftp username, password is their password. ftp command1 is any command line ftp commands like "get filename.zip" (download) or "put filename.rar" (upload).

Now its less important what the contents of the script are, and more important how to execute the script because once you know how, you can go use the commandline ftp client to run through whatever you want to automate, and make sure you have one command per line.

The only differences with the next command are that the hostname is embedded inside the script, and I replaced hostnames, usernames and passwords (etc) with more real examples.

ftp -s:script2.txt

script2.txt
open 192.168.0.123
gus
secretp123
put magicfile.zip
quit


LINUX

At the linux shell (command-line) type this command.
ftp < script2.txt

Because linux just uses i/o redirection, with the < symbol, one should use the format of script2 where the first line of the file is 'open [hostname]'.

USEFUL FTP COMMANDS
SERVER COMMANDS
# cd change directory on the remote SERVER
# cd .. change directory up one level on the remote SERVER
# pwd print the current directory you are within on the remote SERVER
# ls list files within the current directory on the remote SERVER
# binary prepare FTP to transfer binary files such as apps or images
# ascii prepare FTP to transfer ascii or text files such as .html files
# put copy a specific file from the local machine to the remote SERVER
# get copy a specific file from the remote SERVER to the local PC
# chmod change file permissions on a remote SERVER if you have access
# del delete a specific file on the remote SERVER
# bye end your FTP connection

LOCAL MACHINE
# lcd change directory on your local PC
# lcd .. change directory up one level on your local PC
# lpwd print the current directory you are within on your local PC


sources: http://support.microsoft.com/?kbid=96269 and http://www.reallylinux.com/docs/autoftp.shtml

No comments: