How to Write an FTP Script

FTP stands for file transfer protocol. It is exactly what it implies, a way in which files are transferred or fetched across the Internet. Normally an FTP session is used to connect to a server or a remote machine to upload and download files as well as synchronizing directories. Synchronization involves making sure that the directory on the local and remote computers have the same data. Writing an FTP script automates these functions and makes work processes flow smoother.

Why automate FTP sessions? If you are a system administrator or even if you're just using FTP to update files to your website's host server from your local server, you will undertake a number of tasks that are routine and repetitive. If you schedule these tasks in a script and set it to run either during system startup or as a cron job it can save valuable time. A cron job is used in flavors of Unix to schedule programs, events or applications that need to be run at a certain time. Whether you run a program every day or every other month, automating the task through scripting can save significant time. For instance, you can write a script that connects to your server, uploads files and transfers them to your website server.

Things You'll Need

  • Computer
  • Guide to basic shell scripting and windows scripting
  • Notepad (an application that is found on all PC installations)
  • A software program to install an FTP client if one is not already loaded
Show More

Instructions

  1. Writing a simple FTP script for work automation

    • 1

      Learn FTP commands. FTP is a basic program that is simple to use. The basic commands that you will most likely need to use will be open (initiate a login session with the remote [host] machine), lcd (local change directory - changes the directory on your local machine), get (download a file from remote computer), put (upload file to remote computer), cd (change directory - to change directories on the remote system), mget (download multiple files from the remote computer), mput (upload multiple files) pwd (print working [current] directory), ls (list files in the current directory) rmdir (remove the remote directory) and quit (leave the remote FTP session).

    • 2

      Create a script (in Windows). Once you know what you want to do it's a simple process to write the script. You will be writing the script using Notepad. If notepad is not already on your start menu access it by typing "start>all programs>accessories>notepad". Notepad is used for all windows scripting unless another program is specified.

      Sample script - Windows (using notepad)

      After opening a notepad session you will type the following information. All parentheticals below are for informational purposes and should not be a part of the script.

      open ftp.calgaryu.edu (ip address or hostname)
      dmdixo00 (username)
      %%bogieAB2 (password)
      cd \user\home\bobfiles (directory where the files to be downloaded are saved)
      mget *.jpeg - (get all files with the jpeg extension that exists in the directory bobsfiles)
      quit

      Save the file as something like getjpegs.ftp. You will save the file to your current working directory or a directory that has been specified beforehand. In professional settings, work directories will have already been set up. Wherever you save the file, it is important that you know the absolute path to access.

    • 3

      Create a script - Unix. All scripts in Unix will be created in the vi editor. This article assumes some familiarity with vi. If you are unfamiliar please see the resource section for more information about getting started with vi. To create the script, type "vi bobfiles.ftp" on the command line. When the editor window opens, type in the following text.

      #!/bin/ksh

      >
      >
      >ftp -i ftp.calgary.edu *
      >cd /user/home/bobfiles
      >mget *.jpeg
      >quit
      >EOF
      >echo "I got the files"

      Save the file by typing ":wq". The file has already been named at the beginning. To make bobfiles.ftp executable, you will type "chmod 755 bobfiles.ftp" at the command prompt. Though Unix does have some graphical user interfaces, all commands here are to be typed from the command line. Again this assumes a familiarity with Unix. Please see the resource section to find more information on understanding basic Unix.

    • 4

      Configure the .netrc file

      This is an additional step when writing a Unix file. However, after you've done it once, you only have to update the information if you add another machine or update IP addresses. .Netrc is a configuration file that will be used by the Unix script. If the configuration file is present, the system will check it when the FTP script is run. If the file is not present, then you can create it using the vi editor. Type "vi .netrc". The file permissions for .netrc are restricted to 600. To set the permissions type "chmod 600 $HOME/.netrc" at the Unix command line. When you run the FTP script, the machine name in the.netrc file will be read and the associated username and password will be passed to the FTP site. The information in the .netrc should be similar to the example below where "machine" is the name of the computer or domain you are accessing. Login and password refer to the username and password that are being passed to the FTP site from the .netrc file.

      >machine ftp.calgaryu.edu login dmdixo00 password %%bogieAB2

    • 5

      Run the scripts

      In Windows

      After saving the file, you must run it. Please note the file will be saved in your current working directory or a preset directory where it is easily accessible. In Windows, you can set the file to run at startup or set it up to run at set times. The command to run the file in Windows is
      "ftp.exe -s "B:\getjpegs.ftp" ".

      Unix

      Run the file by typing the filename bobfiles.ftp on the command line and hit enter. You can run it from the command line, set it up to run as a startup script or schedule it as a cron job to run at a specific time.

      FTP offers the option of opening an ad hoc FTP session to fetch files without creating a script. However, if you have FTP tasks that are repetitive, then creating a script can automate the task and save valuable time.

Tips & Warnings

  • Unix accesses files by having the path to those files predefined in a configuration file. If for some reason the file or directory you need is not in the configuration file, you will have to type in what is called the absolute path. As time goes on you will learn more secure ways to handle hosts and security information. For more reading look up /etc/hosts, /etc/shadow and sftp - a secure form of FTP.

  • If you are a novice and you are just learning, and you are in a work environment, ask a senior administrator to set up a test environment or test server to fiddle around with. Screwing up the system and causing a work stoppage in the middle of a work day is an epic #FAIL. Even if you're just messing about with your own files for something that seems innocuous, be careful. Scripting is an extremely powerful tool.

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Check the Status of a Unix FTP Transfer

    The File Transfer Protocol is a standard means to transfer files between computers across the Internet. The standard FTP client installed on...

  • How to Write CMD Line FTP Scripts

    FTP, which stands for File Transfer Protocol, is a way to rapidly transfer files on the Web. While the HyperText Transfer Protocol...

  • How to Write a Radio Script

    Learning how to write a radio script is critical for proper execution of a radio performance. The script must include various cues...

  • How to Write in Shell Script

    Learning the command line in Linux is always the first step for a new Linux user. If you want to start doing...

  • How to FTP a File Using VBS

    VBS or Visual Basic Script is a scripting language that can be used for client side web development in Internet Explorer, server...

  • How to Create an FTP Script

    A File Transfer Protocol (FTP) script is a simple batch script that uses the FTP program built into Microsoft Windows to automate...

  • How to Create a Script File on Unix

    A Unix script file is a plain text file that contains a list of commands that are executed in order. It is...

  • How to Write a TV Commercial Script

    Television commercials sell products. All of the flashy images, effects and dialogue you see and hear on the television screen start with...

  • How to Write a Shell Script in Unix

    Under UNIX, and UNIX-like operating systems such as Linux and Mac OS X, shell scripting makes the automation of tasks much easier....

  • How to Write a Script in Unix

    In Unix, the command line gives you access to a wide range of tools, as well as the ability to combine them...

Related Ads

Featured