Speedtest CLI: Command Line Guide For Network Testing

by Jhon Lennon 54 views

Hey there, network enthusiasts! Ever wanted to run a speed test right from your terminal? Well, you're in the right place. This guide dives into the Speedtest CLI, a command-line interface for the popular Speedtest.net service. We'll explore its commands, options, and how you can use it to monitor your network performance like a pro. So, buckle up and let's get started!

What is Speedtest CLI?

Speedtest CLI is the command-line version of the well-known Speedtest.net, a service provided by Ookla. It allows you to perform internet speed tests directly from your terminal or command prompt. Instead of opening a web browser, navigating to the Speedtest.net website, and clicking on the 'Go' button, you can simply execute a command in your terminal and get your download speed, upload speed, and ping results. This is super handy for network admins, developers, or anyone who prefers a more direct and automated way to test their internet connection.

Why use the Speedtest CLI? Well, there are several compelling reasons. First off, it's automation-friendly. You can integrate it into scripts to automatically test your internet speed at regular intervals and log the results. This is invaluable for monitoring network performance over time. Secondly, it's great for remote servers. If you're managing a server that doesn't have a GUI, the Speedtest CLI is a lifesaver. You can quickly check the server's internet speed without needing a graphical interface. Lastly, it's incredibly convenient. For those who spend a lot of time in the terminal anyway, it's much faster to run a quick speed test via the command line than to switch to a browser.

Installation

Before diving into the commands, you'll need to install the Speedtest CLI. The installation process varies depending on your operating system. Here's a rundown for different platforms:

macOS

If you're on macOS, the easiest way to install Speedtest CLI is using Homebrew, a popular package manager. If you don't have Homebrew installed, you can get it from brew.sh. Once you have Homebrew, open your terminal and run:

brew install speedtest-cli

This command will download and install the Speedtest CLI along with any necessary dependencies. After the installation is complete, you can verify it by running speedtest in your terminal. If it's installed correctly, you should see the Speedtest CLI output.

Linux

For Linux users, the installation process depends on your distribution. On Debian-based systems like Ubuntu, you can use apt-get. First, you might need to update your package list:

sudo apt-get update

Then, install the Speedtest CLI:

sudo apt-get install speedtest-cli

On Fedora or other Red Hat-based systems, you can use dnf:

sudo dnf install speedtest-cli

Alternatively, you can often download a standalone binary from the Speedtest CLI website and place it in your /usr/local/bin directory. Make sure to make it executable using chmod +x /usr/local/bin/speedtest. This method is useful if your distribution's package manager doesn't have the latest version.

Windows

Windows users can download the Speedtest CLI executable from the official Speedtest CLI website. Once you've downloaded the executable, place it in a directory like C:\Windows\System32 or add it to your system's PATH environment variable. Adding it to the PATH allows you to run the speedtest command from any directory in the command prompt. After placing the executable, open a new command prompt and type speedtest to verify the installation.

Basic Commands

Now that you have Speedtest CLI installed, let's explore some basic commands to get you started. The most fundamental command is simply:

speedtest

Running this command will initiate a speed test using the default settings. The CLI will automatically select the nearest Speedtest server, measure your download speed, upload speed, and ping, and display the results in your terminal. The output usually includes the ping in milliseconds, the download speed in Mbps (Megabits per second), and the upload speed in Mbps. It also shows the server location and the ISP (Internet Service Provider) associated with your connection.

Displaying Results in Bytes

By default, Speedtest CLI displays speeds in Mbps. If you prefer to see the results in bytes instead of bits, you can use the --bytes option:

speedtest --bytes

This command will show the download and upload speeds in MB/s (Megabytes per second). This can be useful if you're more familiar with measuring data transfer in bytes, or if you're comparing the results with other tools that use bytes as the default unit.

Sharing Results

Speedtest CLI allows you to share your test results by uploading them to Speedtest.net. To do this, use the --share option:

speedtest --share

After the test completes, the CLI will generate a shareable URL that you can use to view your results on the Speedtest.net website. This is handy for sharing your results with friends, colleagues, or your ISP if you're experiencing issues with your internet connection. The shared results page provides a visual representation of your speed test data, including the ping, download speed, and upload speed, along with a map showing the location of the test server.

Advanced Options

Beyond the basic commands, Speedtest CLI offers a range of advanced options that allow you to customize your speed tests. These options can be particularly useful for troubleshooting network issues or for specific testing scenarios.

Selecting a Specific Server

By default, Speedtest CLI automatically selects the nearest server for testing. However, you can manually specify a server using the --server option followed by the server ID. To find a list of available servers and their IDs, you can use the --list option:

speedtest --list

This command will display a list of Speedtest servers along with their IDs, names, and distances from your location. Once you have the ID of the server you want to use, you can specify it in your speed test command:

speedtest --server 1234

Replace 1234 with the actual server ID. This can be useful if you want to test your connection to a specific location, or if you suspect that the default server is not providing accurate results.

Getting a Simple Output

If you only need the ping, download, and upload speeds without any extra information, you can use the --simple option:

speedtest --simple

This command will display the results in a simplified format, making it easier to parse the output in scripts or automated processes. The output will typically consist of three lines: the ping in milliseconds, the download speed in Mbps, and the upload speed in Mbps.

Performing a Ping-Only Test

Sometimes, you might only be interested in measuring the ping (latency) of your connection. You can do this using the --ping option:

speedtest --ping

This command will perform a ping test to the selected server and display the result in milliseconds. This can be useful for diagnosing network latency issues or for testing the responsiveness of your connection.

Specifying a Source IP Address

In certain network configurations, you might need to specify a source IP address for the speed test. You can do this using the --source option followed by the IP address:

speedtest --source 192.168.1.100

Replace 192.168.1.100 with the actual IP address you want to use. This is particularly useful if you have multiple network interfaces or if you're testing from a server with multiple IP addresses.

Automating Speed Tests

One of the most powerful features of Speedtest CLI is its ability to be automated. You can integrate it into scripts or cron jobs to automatically test your internet speed at regular intervals and log the results. This is invaluable for monitoring network performance over time and identifying potential issues.

Using Cron Jobs

On Linux and macOS systems, you can use cron jobs to schedule speed tests. To edit your crontab, run:

cron -e

This will open the crontab file in a text editor. Add a line like the following to schedule a speed test every hour:

0 * * * * speedtest --simple >> /path/to/speedtest.log

This will run the speedtest --simple command at the beginning of every hour and append the output to the /path/to/speedtest.log file. Make sure to replace /path/to/speedtest.log with the actual path to your log file.

Creating a Script

You can also create a script to run speed tests and process the results. Here's a simple example in Bash:

#!/bin/bash

DATE=$(date +"%Y-%m-%d %H:%M:%S")
PING=$(speedtest --simple | grep Ping | awk '{print $2}')
DOWNLOAD=$(speedtest --simple | grep Download | awk '{print $2}')
UPLOAD=$(speedtest --simple | grep Upload | awk '{print $2}')

echo "$DATE, Ping: $PING ms, Download: $DOWNLOAD Mbps, Upload: $UPLOAD Mbps" >> /path/to/speedtest.log

This script runs a speed test using the --simple option, extracts the ping, download, and upload speeds, and appends the results to a log file along with a timestamp. You can then schedule this script to run at regular intervals using a cron job.

Troubleshooting

While Speedtest CLI is generally reliable, you might encounter issues from time to time. Here are some common problems and their solutions:

Command Not Found

If you get a "command not found" error when running speedtest, it usually means that the Speedtest CLI executable is not in your system's PATH. Make sure that the directory containing the executable is added to your PATH environment variable. On Windows, you can do this through the System Properties dialog. On Linux and macOS, you can modify your .bashrc or .zshrc file.

Connection Errors

If you encounter connection errors, such as "Could not connect to server," it could be due to a firewall or network issue. Make sure that your firewall is not blocking the Speedtest CLI from accessing the internet. You can also try selecting a different server using the --server option.

Inaccurate Results

If you suspect that the speed test results are inaccurate, try running the test multiple times at different times of the day. Network congestion can affect the results, so it's best to get an average over time. You can also try selecting a different server using the --server option, as some servers might be more reliable than others.

Conclusion

The Speedtest CLI is a powerful and versatile tool for measuring your internet speed from the command line. Whether you're a network admin, a developer, or just a curious user, it offers a convenient and automated way to monitor your network performance. By mastering the basic commands and advanced options, you can gain valuable insights into your internet connection and troubleshoot potential issues. So go ahead, give it a try, and take control of your network testing! Happy testing, guys!