/ Bash Scripts

Automate FTP transfers

This guide is designed to walk you through the process of creating a Bash script for automating File Transfer Protocol (FTP) transfers. The script will have the capability to integrate user-provided custom options and settings.

Before we start, please ensure you have the necessary privileges to execute scripts on your Linux environment. If you are unsure, refer to the user documentation of your specific Linux distribution.

Script Preparation

Before you start writing your script, you need to identify the requirements and parameters. Here, we’re looking at automating FTP transfers, which generally involve:

  1. The FTP server address.
  2. Credentials for the server (username and password).
  3. The operation to perform (upload or download).
  4. The source and destination files or directories.

To enable custom settings, we’ll use command-line arguments for these parameters.

Writing the Script

Open your favorite text editor, and create a new file named ftp_auto.sh. Below is the basic structure of the script:

#!/bin/bash

# Usage function
usage() {
  echo "Usage: $0 [-s server] [-u username] [-p password] [-o operation] [-f file] [-d directory]"
  echo "  -s: FTP server address"
  echo "  -u: FTP username"
  echo "  -p: FTP password"
  echo "  -o: operation (get or put)"
  echo "  -f: file (source file for 'put', target filename for 'get')"
  echo "  -d: directory (remote directory)"
  exit 1
}

# Parse command-line options
while getopts "s:u:p:o:f:d:" opt; do
  case ${opt} in
    s) server=${OPTARG} ;;
    u) username=${OPTARG} ;;
    p) password=${OPTARG} ;;
    o) operation=${OPTARG} ;;
    f) file=${OPTARG} ;;
    d) directory=${OPTARG} ;;
    \?) usage ;;
  esac
done

# Check if all parameters are provided
if [ -z "$server" ] || [ -z "$username" ] || [ -z "$password" ] || [ -z "$operation" ] || [ -z "$file" ] || [ -z "$directory" ]; then
  usage
fi

# FTP operations
ftp -n -v $server << EOF
user $username $password
cd $directory
$operation $file
bye
EOF

Explaining the Script

Here’s what each section does:

  1. The script begins with #!/bin/bash, known as the shebang, telling the system this is a Bash script.
  2. The usage function defines how to use the script and which options it requires.
  3. getopts is used to read the command-line arguments provided to the script. If any option is missing or incorrect, the usage function is called.
  4. Before executing the FTP commands, the script checks if all required parameters are provided. If not, it calls the usage function and exits.
  5. The FTP command is used with a here document (<< EOF) to execute multiple FTP commands in one session.

Script Permissions

Save the script and give it execute permissions:

chmod +x ftp_auto.sh

Script Usage

To use the script, call it with the required parameters:

./ftp_auto.sh -s ftp.example.com -u myusername -p mypassword -o put -f myfile.txt -d /remote/directory/

Caution

  • FTP transfers data in plain text. For a more secure approach, consider using SFTP or SCP.
  • Storing passwords directly in scripts or passing them as command-line arguments can expose them to other users on the system and should be avoided in a production environment. Consider using .netrc file or environment variables to store credentials securely.

This guide provides you with a basic but flexible solution for automating FTP transfers. This script can be further enhanced to fit more specific needs or to add additional security measures.

Was this helpful?

Thanks for your feedback!