How to run a speed test using the Linux command line

This guide will show you how to run an Internet speed test from the Linux command-line interface (CLI) and cron. You will also see a sample Jenkinsfile if you want to use a Jenkins for the automation instead of cron.

Speed test command-line options

Several services offer a speed test service via the CLI. Arguably, the most notable of these are speedtest.net by Ookla. You could also use fast.com by Netflix. This guide will show you how to use the speed test CLI service provided by Ookla. So visit https://github.com/sindresorhus/fast-cli for details on using the CLI for Fast.

Speed test CLI from Ookla

Many people trust Ookla’s Speedtest CLI with its global server network. As a result, the command-line tools are well suited for DevOps.

The Speedtest CLI allows you to do the following:

  • Measure your Internet connection’s download, upload, latency, and packet loss.
  • Do a speed test without relying on a web browser.
  • Install the Speedtest CLI on macOS, Debian/Ubuntu, Fedora/Centos/Redhat, and FreeBSD.
  • Set up automated scripts to collect connection performance data.
  • View test results via CSV, JSONL, or JSON.

Install Speedtest CLI

The following install instructions is a summary from speedtest.net:

macOS

Use the following commands to install Speedtest CLI on macOS:

brew tap teamookla/speedtest
brew update
brew install speedtest --force

Debian/Ubuntu

You can use the following commands to install Speedtest CLI on Debian/Ubuntu:

curl -s https://install.speedtest.net/app/cli/install.deb.sh | sudo bash
sudo apt-get install speedtest

Fedora/Centos/Redhat

You can use the following commands to install Speedtest CLI on Fedora/Centos/Redhat:

curl -s https://install.speedtest.net/app/cli/install.rpm.sh | sudo bash
sudo yum install speedtest

FreeBSD

You can use use the following commands to install Speedtest CLI on FreeBSD:

sudo pkg update && sudo pkg install -g libidn2 ca_root_nss
sudo pkg add "https://install.speedtest.net/app/cli/ookla-speedtest-1.0.0-freebsd.pkg"

Run the speed test using the CLI

The following command will run the speed test:

speedtest-cli

Result:

Retrieving speedtest.net configuration...
Testing from Starlink (103.150.126.32)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Over the Wire (Sydney) [1.32 km]: 39.851 ms
Testing download speed................................................................................
Download: 57.32 Mbit/s
Testing upload speed......................................................................................................
Upload: 13.59 Mbit/s

The following command will run the speed test and produce JSON output:

speedtest-cli --json

Result:

{"download": 45898976.19254001, "upload": 17945425.352851078, "ping": 53.928, "server": {"url": "http://speedtest.mirror.digitalpacific.com.au:8080/speedtest/upload.php", "lat": "-33.8600", "lon": "151.2111", "name": "Sydney", "country": "Australia", "cc": "AU", "sponsor": "Digital Pacific", "id": "5744", "host": "speedtest.mirror.digitalpacific.com.au:8080", "d": 1.3224649556794528, "latency": 53.928}, "timestamp": "2021-10-23T20:22:26.022297Z", "bytes_sent": 24059904, "bytes_received": 57511476, "share": null, "client": {"ip": "103.150.126.32", "lat": "-35.8672", "lon": "161.1997", "isp": "Starlink", "isprating": "3.7", "rating": "0", "ispdlavg": "0", "ispulavg": "0", "loggedin": "0", "country": "AU"}}

Choose a speed test server

You can choose a specific speed test server using the following command:

speedtest-cli --list

Result:

Retrieving speedtest.net configuration...
 2173) Internode (Sydney, Australia) [1.32 km]
 ...

Consequently, you can then specify your preferred server using the following command:

speedtest-cli --server 2173

Does speedtest-cli not execute when scheduled using cron?

There are a few reasons why the speedtest-cli command may fail when running it via cron:

  • First, make sure the path to speedtest-cli is set. Thus call: /usr/bin/speedtest-cli instead of just speedtest-cli.
  • Next, make sure you set “#!/bin/bash” in your bash script.

How to execute the speedtest-cli via Jenkins

The following script runs the speedtest-cli via a Jenkins pipeline and then uploads the JSON file to AWS S3.

pipeline {

    agent any

    stages {
        
        stage('Run test') {
            
            steps {
                
                    echo "Send speedtest data to: ${WORKSPACE}/${currentBuild.startTimeInMillis}.json"

                    echo "Create the script file"
                    script {
                        
                       def data = "#!/bin/bash\n/usr/bin/speedtest-cli --json > ${WORKSPACE}/${currentBuild.startTimeInMillis}.json"
                       writeFile(file: 'test.sh', text: data)  
                       
                    }
                    
                    echo "Set the execute permissions"
                    sh "chmod +x test.sh"
                    
                    echo "Run the test script"
                    sh "./test.sh"

            }
            
        }
        
        stage('Upload to S3') {
            
            steps {
                
                    echo "Upload ${currentBuild.startTimeInMillis}.json s3://speedtest-results-starlink/ to S3"
                    sh "aws s3 cp ${currentBuild.startTimeInMillis}.json s3://speedtest-results-starlink/"

            }
            
        }        

    }
    
    post { 
        always { 

            echo 'Cleaup'
            sh "rm ${WORKSPACE}/*.json"

        }
        
    }    

}

Wrapping up

You can now run an Internet speed test from the Linux command-line interface (CLI), cron, and Jenkins.

You may also be interested in

Source(s):



About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee



About Anto Online

Having started his career in 1999 as a Desktop Support Engineer, Anto soon changed paths and became a developer. After several years of development experience, he transitioned into a consultant. As an enterprise application consultant for a leading SaaS software provider, Anto specializes in AWS's serverless technologies. By day, Anto focuses on helping customers leverage the power of serverless technologies. By night, he indulges his passion for cloud computing by playing with Python and trying out things that are currently beyond the scope of his work. Sometimes Anto needs help as there are not enough hours at night. So Anto relies on a team of fellow Cloud enthusiasts to help him out. Each one is a Cloud expert in their own right, and Anto takes great pride in helping them learn and grow.

View all posts by Anto Online →

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.