Skip to content

Instantly share code, notes, and snippets.

@linosteenkamp
Last active August 25, 2018 20:50
Show Gist options
  • Save linosteenkamp/df8b26f0cfcb34f51a742000c45a8153 to your computer and use it in GitHub Desktop.
Save linosteenkamp/df8b26f0cfcb34f51a742000c45a8153 to your computer and use it in GitHub Desktop.
Execute speedtest-cli and write results to speedtest-api
#!/bin/bash
# URL for speedtest-api (Change this to match your setup)
URL="localhost:3000/api/test/create"
# Path to speedtest-cli (Change this to match the path to your speedtest-cli executable)
SPEEDTEST_CLI_PATH="/usr/local/bin/speedtest-cli"
# Input Field Seperator for splitting the CSV result into an array
IFS=","
# Do Speed Test
result=$($SPEEDTEST_CLI_PATH --csv)
# Result to Array
arr=($result)
# Transform upload and download values to mbit/s
arr[6]=$(awk "BEGIN {print ${arr[6]}/1000000}")
arr[7]=$(awk "BEGIN {print ${arr[7]}/1000000}")
# Create JSON record
json="{ \
\"server_id\":${arr[0]}, \
\"sponsor\":\"${arr[1]}\", \
\"server_name\":\"${arr[2]}\", \
\"timestamp\":\"${arr[3]}\", \
\"distance\":${arr[4]%.*}, \
\"ping\":${arr[5]%.*}, \
\"download\":${arr[6]}, \
\"upload\":${arr[7]}, \
\"share\":null, \
\"ip_address\":\"${arr[9]}\" \
}"
curl -i \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data "${json}" "${URL}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment