}

How to check internet speed on a Linux server from the terminal using cli?

Created:

Intruction

Sometimes you don't have a visual interface to execute a browser and use a internet speed test. In this tutorial we are going to explain how to measure internet speed using the command line. The software we are going to use is open source and is available here, it uses speedtest.net. The command speedtest-cli will test the internet connection to the closest server.

We will also save all speed test in a csv file to analize server bandwith. Note this tutorial will also work on OSX and Windows, since we will use a Python project called speedtest-cli.

Step 1: Install speedtest-cli

sudo apt update
sudo apt install python-pip
pip install speedtest-cli

If you have permission issues with pip, try to use the --user parameter

Step 2: Using speedtest command

After you install speedtest-cli you will have the command speedtest available.

speedtest

The execution of the command will output something like:

Retrieving speedtest.net configuration...
Testing from Digital Ocean (138.197.7.114)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Sprint (Harrison, NJ) [10.20 km]: 2.377 ms
Testing download speed................................................................................
Download: 3026.58 Mbit/s
Testing upload speed................................................................................................
Upload: 1849.93 Mbit/s

Saving a csv

The command speedtest has a parameter --csv which will print a new line (or row) that we will append to a file. Check the following example:

speedtest --csv

The output should be similar to:

10390,Speedtest.net,"New York City, NY",2018-06-22T04:34:29.945140Z,16.976263463593583,2.699,652996846.7973796,459352903.67766047,,138.197.7.114

In order to save historical data we are going to use cron. Create a new entry using the command crontab -e and append the following line:

*/5 * * * * speedtest --csv >> /home/usernamme/speedtest.csv 2>&1

The cron line above will execute the every five minutes the speedtest and append the new row to the speedtest.log file. Make sure you change the frequency on the cron to a bigger one to reduce bandwidth usage.

Plotting the hisitorical data

The easiest way is to open the csv with google docs and make a plot from there.

Use the following python script to plot the historical internet speed of your linux server:

10390,Speedtest.net,"New York City, NY",2018-06-22T04:34:29.945140Z,16.976263463593583,2.699,652996846.7973796,459352903.67766047,,138.197.7.114

import pandas as pd
from datetime import datetime
import csv
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
headers = ['Server ID', 'Sponsor', 'Server Name', 'Timestamp', 'Distance',
               'Ping', 'Download', 'Upload', 'Share', 'IP Address']
df = pd.read_csv('/home/username/speedtest.csv',names=headers)
print (df)

df['Timestamp'] = df['Timestamp'].map(lambda x: datetime.strptime(str(x), '%Y/%m/%d %H:%M:%S.%f'))
x = df['Timestamp']
y = df['Download']

# plot
plt.plot(x,y)
plt.gcf().autofmt_xdate()

plt.show()

Out plot scripts uses pandas to read the csv file. We use headers varible to define column names. We then choose timestamp and download as x and y series for out plot. You can also plot Upload instead.

That's all, please leave a comment if you want to improve our internet speed tutorial or if you have any doubts.