How to create a Bash script to test FTP

How to create a Bash script to test FTP

In this article, we will explore how to create a Bash script to test the FTP connection to a specific host.

Bash scripting is a powerful tool for automating tasks and testing connections to various services, including those based on the File Transfer Protocol (FTP). In this article, we will explore how to create a Bash script to test the FTP connection to a specific host.

Before we begin, let's ensure that the FTP client is installed on the system. On Debian or Ubuntu-based systems, we can do this with the following command:


sudo apt-get install ftp

On Red Hat or CentOS-based systems, we can use:


# Or by using dnf
sudo yum install ftp

Let's create a Bash script called test_ftp_connection.sh that will allow us to test the FTP connection. Use a text editor like nano or vim to create the file:


nano test_ftp_connection.sh

Now, insert the following code into the script:


#!/bin/bash

# Variable declarations

HOST="192.168.1.8"
USERNAME="user"
PASSWORD="password"
TEST_FILE="test_file.txt"

if [ ! -f $TEST_FILE ]; then
    echo "Creating test file..."
    echo "This is a test file." > $TEST_FILE
    echo "Test file created successfully."
else
    echo "Test file already exists."
fi

check_ftp_client() {
    # Check for the presence of the FTP client
    if [ -x "$(command -v ftp)" ]; then
        echo "FTP client found."
    else
        echo "FTP client not found. Install the FTP client to run the test."
        exit 1
    fi
}

# Call the FTP client check function

check_ftp_client

# Function to test the FTP connection
test_ftp_connection() {
    # FTP connection and status check
    ftp -n $HOST <<END_SCRIPT
    quote USER $USERNAME
    quote PASS $PASSWORD
    quit
END_SCRIPT

    # Check the exit status of the ftp command
    if [ $? -eq 0 ]; then
        echo "FTP connection successful!"
        # Create a remote test file
        echo "This is a test file." | ftp -n $HOST <<END_SCRIPT
        quote USER $USERNAME
        quote PASS $PASSWORD
        put $TEST_FILE
        quit
END_SCRIPT
        echo "Test file created successfully."
    else
        echo "FTP connection failed. Check credentials and service availability."
    fi

    echo "Deleting remote test file..."
    # Delete the remote test file
    ftp -n $HOST <<END_SCRIPT
    quote USER $USERNAME
    quote PASS $PASSWORD
    delete $TEST_FILE
    quit
END_SCRIPT
    echo "Remote test file deleted successfully."

    # Delete the local test file
    echo "Deleting local test file..."
    rm $TEST_FILE
    echo "Local test file deleted successfully."
    exit 0
}

# Call the FTP connection test function
test_ftp_connection

The script will attempt to upload a local file to the remote FTP server, verifying the outcome of the operation. The first check involves the local file, which will be created if it does not exist. Then, the local installation of the FTP client is checked. If there is no local FTP client, the script will terminate its execution. Next, an FTP session is created with the remote server, and the test file is uploaded, which, once the verification is complete, will be removed from both the remote server and locally. The FTP session involves using FTP commands such as put and delete.

Before running the script, make sure to make it executable with the following command:


chmod +x test_ftp_connection.sh

Now, you can run the script:


./test_ftp_connection.sh

Make sure to customize the variables in the script header with the correct information for the FTP host, username, and password.