Skip to content

Instantly share code, notes, and snippets.

@milojennings
Forked from dvdsmpsn/speedtest-ifttt.sh
Last active March 25, 2018 20:58
Show Gist options
  • Save milojennings/316a821a9bc1c1141ffefe46f08c3be1 to your computer and use it in GitHub Desktop.
Save milojennings/316a821a9bc1c1141ffefe46f08c3be1 to your computer and use it in GitHub Desktop.
Modified version of Henrik Bengtsson's speedtest-cli code which will dispatch the test results to Zapier
#!/usr/bin/env bash
###########################################################################
# Originally written by: Henrik Bengtsson, 2014
# https://github.com/HenrikBengtsson/speedtest-cli-extras
# Modified to use Zapier by: Milo Jennings, 2018
# Further modified to store a permanent log locally with all the details in
# a CSV file on the desktop (designed for Mac use)
# License: GPL (>= 2.1) [http://www.gnu.org/licenses/gpl.html]
# See Google Sheet Template: https://docs.google.com/spreadsheets/d/1usJKKMoUpt-VWI94aJ_rU4qLj3ImjDoNt6HqFL-wcRE/edit?usp=sharing
# -------------------------------------------------------------------------
# USAGE:
# git clone https://github.com/HenrikBengtsson/speedtest-cli-extras.git
# Copy to speedtest-cli-extras/bin/speedtest-csv to /usr/local/bin/speedtest-cli
# Create a Zapier workflow with a webhook that adds the entries to a copy of the google sheet template mentioned above
# Setup a cronjob that runs this script
###########################################################################
# Character for separating values
# (commas are not safe, because some servers return speeds with commas)
sep=";"
# Temporary file holding speedtest-cli output
user=$USER
if test -z $user; then
user=$USERNAME
fi
log="/Users/$user/tmp/speedtest-csv.log"
permanent_log="/Users/$user/Desktop/speedtest-log.csv"
# Local functions
function str_extract() {
pattern=$1
# Extract
res=`grep "$pattern" $log | sed "s/$pattern//g"`
# Drop trailing ...
res=`echo $res | sed 's/[.][.][.]//g'`
# Trim
res=`echo $res | sed 's/^ *//g' | sed 's/ *$//g'`
echo $res
}
# Display header?
if test "$1" = "--header"; then
start="start"
stop="stop"
from="from"
from_ip="from_ip"
server="server"
server_dist="server_dist"
server_ping="server_ping"
download="download"
upload="upload"
share_url="share_url"
else
mkdir -p `dirname $log`
start=`date +"%Y-%m-%d %H:%M:%S"`
if test -n "$SPEEDTEST_CSV_SKIP" && test -f "$log"; then
# Reuse existing results (useful for debugging)
1>&2 echo "** Reusing existing results: $log"
else
# Query Speedtest
/usr/local/bin/speedtest-cli --share > $log
fi
stop=`date +"%Y-%m-%d %H:%M:%S"`
# Parse
from=`str_extract "Testing from "`
from_ip=`echo $from | sed 's/.*(//g' | sed 's/).*//g'`
from=`echo $from | sed 's/ (.*//g'`
server=`str_extract "Hosted by "`
server_ping=`echo $server | sed 's/.*: //g'`
server=`echo $server | sed 's/: .*//g'`
server_dist=`echo $server | sed 's/.*\\[//g' | sed 's/\\].*//g'`
server=`echo $server | sed 's/ \\[.*//g'`
download=`str_extract "Download: "`
upload=`str_extract "Upload: "`
share_url=`str_extract "Share results: "`
fi
# Standardize units?
if test "$1" = "--standardize"; then
download=`echo $download | sed 's/Mbits/Mbit/'`
upload=`echo $upload | sed 's/Mbits/Mbit/'`
fi
# Dump to local CSV file using semicolons are separator
if [[ ! -f $permanent_log ]]; then
echo -e "Time Stamp;Server;Server Ping;Server Distance;Download;Upload;From;From IP;Share URL" > $permanent_log
fi
echo -e "${start};${server};${server_ping};${server_dist};${download};${upload};${from};${from_ip};${share_url}" >> $permanent_log
# # Send to IFTTT
# secret_key=""
# value1=`echo $server_ping | cut -d" " -f1`
# value2=`echo $download | cut -d" " -f1`
# value3=`echo $upload | cut -d" " -f1`
# json="{\"value1\":\"${value1}\",\"value2\":\"${value2}\",\"value3\":\"${value3}\"}"
# echo $json
# curl -X POST -L -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key} -i -v --insecure
# Send to Zapier
zapier_hook_url="" #FILL THIS IN
from="$from"
from_ip="$from_ip"
server="$server"
server_ping=`echo $server_ping | cut -d" " -f1`
server_dist="$server_dist"
share_url=`echo $share_url | cut -d" " -f1`
download=`echo $download | cut -d" " -f1`
upload=`echo $upload | cut -d" " -f1`
json="{\"server\":\"${server}\",\"share_url\":\"${share_url}\",\"server_dist\":\"${server_dist}\",\"from\":\"${from}\",\"from_ip\":\"${from_ip}\",\"server_ping\":\"${server_ping}\",\"download\":\"${download}\",\"upload\":\"${upload}\"}"
echo $json
curl -X POST -L -H "Content-Type: application/json" -d "${json}" $zapier_hook_url -i -v --insecure
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment