Multi Server Average Internet Speed Test Bash Script

All kinds of 'OS' scripts
Post Reply
Posts: 481
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta

Multi Server Average Internet Speed Test Bash Script

Post by ben53252642 » Tuesday 01 August 2017 15:16

This script uses speedtest-cli for testing internet bandwidth using a server list provided by

Servers are randomly selected, the number of servers to pick is specified at the top of the script, upload and download speeds are displayed and the averages calculated at completion of multiple tests.

You will need to change the Domoticz Configuration section and the idx numbers to correspond to your Domoticz custom sensors in the "Load data into Domoticz" section at the end of the script.

Code: Select all


# Set number of tests to run

# Domoticz Configuration

if [ ! -f speedtest-cli ]; then
echo && echo "Downloading speedtest-cli..." && echo
wget -q -O speedtest-cli
chmod +x speedtest-cli

while true; do

# Get list of speedtest servers and sort format
serverlist=$(python speedtest-cli --list | tail -n +2)
filter=$(echo "$serverlist" | cut -d' ' -f2- | awk '{print $1}' | sed 's/[^0-9]*//g')
usable=$(echo "$filter" | sort -bus | sed '/^$/d')

# Pick number of servers at random
rm -rf /tmp/dl.txt && rm -rf /tmp/up.txt
random=$(echo "$usable" | sort -Rbus | head -n "$numberoftests")

# Run the speed tests - if any errors occur for a server it will automatically be skipped
echo && echo "----------Initiating Speed Tests----------" && echo
echo "$random" | while read -r line; do
test=$(python speedtest-cli --server "$line")
dl=$(echo "$test" | grep 'Download:' | awk '{print $2}')
up=$(echo "$test" | grep 'Upload:' | awk '{print $2}')
if [ "$dl" > "0" ]; then
dlunit=$(echo "$test" | grep 'Download:' | awk '{print $3}')
if [ "$up" > "0" ]; then
upunit=$(echo "$test" | grep 'Upload:' | awk '{print $3}')
if [[ "$dlunit" == "Mbit/s" ]]; then
if [[ "$upunit" == "Mbit/s" ]]; then
servername1=$(echo "$serverlist" | grep -w "$line" | awk '{$1= ""; print $0}' | cut -d' ' -f2-)
echo "$servername1"
echo "$dl" >> /tmp/dl.txt
echo "$up" >> /tmp/up.txt
echo "Download:" "$dl" "$dlunit" "Upload:" "$up" "$upunit" && echo

# Calculate averages - only results in Mbit/s are included
avgdl=$(cat /tmp/dl.txt | xargs | perl -lane '$a+=$_ for(@F);print"".$a/scalar(@F)') && avgdl=$(printf "%.2f\n" "$avgdl")
avgup=$(cat /tmp/up.txt | xargs | perl -lane '$a+=$_ for(@F);print"".$a/scalar(@F)') && avgup=$(printf "%.2f\n" "$avgup")
echo "----------Results----------" && echo
echo "Average Download:" "$avgdl" "Mbit/s"
echo "Average Upload:" "$avgup" "Mbit/s"

# Load data into Domoticz
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=503&svalue=${avgdl}"
curl -s "http://{$domoticzuser}:{$domoticzpass}@{$domoticzserver}:{$domoticzport}/json.htm?type=command&param=udevice&idx=502&svalue=${avgup}"

# Sleep for 6 hours
sleep 21600
By default the script will run every 6 hours specified in seconds at the end of the script, I run it in a screen session so that it continues in the background.
Screen Shot 2017-08-02 at 3.12.54 am.png
Screen Shot 2017-08-02 at 3.12.54 am.png (175.84 KiB) Viewed 842 times

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests