Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Modified version of Henrik Bengtsson's speedtest-cli code which will dispatch the test results to the IFTTT Maker Channel.
#!/usr/bin/env bash
###########################################################################
# Originally written by: Henrik Bengtsson, 2014
# https://github.com/HenrikBengtsson/speedtest-cli-extras
# Modified to use IFTTT by: Alasdair Allan, 2015
# License: GPL (>= 2.1) [http://www.gnu.org/licenses/gpl.html]
###########################################################################
# 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=/tmp/$user/speedtest-csv.log
# 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
# Send to IFTTT
secret_key="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}\"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key}

I'm not super familiar with Bash.... but when I try to run this script - I'm getting:
./speedtest-ifttt.sh: 21: ./speedtest-ifttt.sh: Syntax error: "(" unexpected

It shouldn't have any impact but I have substituted my secret key.

I figured out why it wasn't working... though the sheet is not being created in my gDrive... working on that issue now

Found the sheet, sync issue with my local machine 😒

lubosm commented Feb 25, 2016

:( hmm, still Syntax error: "(" unexpected

speedtest-ifttt.sh: 21: speedtest-ifttt.sh: Syntax error: "(" unexpected

Landei commented Mar 9, 2016

If you see "speedtest-ifttt.sh: 21: speedtest-ifttt.sh: Syntax error: "(" unexpected" you have to remove the "function" in line 21.

or run it with ... /bin/bash /speedtest-ifttt.sh

meaverick commented Oct 13, 2016 edited

sudo ./speedtest-ifttt.sh
./speedtest-ifttt.sh: line 54: /usr/local/bin/speedtest-cli: No such file or directory
Congratulations! You've fired the speedtest event

drijd commented Nov 25, 2016

Hi,

Thanks so much for your efforts in producing this. I got this working in an evening, which given my skill level, I'm delighted with. If I get chance, I'll write something on the IFTTT setup too, unless you have already blogged that somewhere?

Thanks once again,

Ian

vacano commented Nov 29, 2016

Hey,

Great initiative, but cant manage to make it work. After I start it with bash speedtest-ifttt.sh I get the Congratulations! You've fired the speedtest event but not spreadsheed is created on my google drive ?!? Where am I being wrong ?

Thanks

Stybyk commented Feb 13, 2017

Hello,
i have same problem. I try to create exact folder with exact empty spreadsheet.
In addon activity log i have "Applet updated" messages witch "log speedtest results to spreadsheet" but google table is still empty.
Where will be problem ?

babca commented Feb 14, 2017

@Stybyk @vacano tested, it works for me. Current IFFFT web interface is a bit confusing, maybe you have a mistake in your configuration.

Make sure you have "Log Speedtest results to Spreadsheet" applet in https://ifttt.com/my_applets
Click on the applet and then click on "Check now". Is the check working? It should say "Applet checked".
If not, check the applet settings:

event name speedtest
spreadsheet name MyAutoSpeedtest
formatted row {{OccurredAt}}
Drive folder path speedtests/{{EventName}}

Then open Maker service setting page: https://ifttt.com/services/maker/settings
You will see an account info:

Account Info
Connected as your nickname
URL https://maker.ifttt.com/use/__________YOUR_KEY__________
Status active

Select and open your URL in your browser to test your applet manually.
Fill the inputs with test data: {event} = speedtest, value1 = 1, value2 = 1, value3 = 1 and submit with Test it button.
You will see a new spreadsheet in sheets.google.com with 1, 1, 1 data row.

Finally, copy/paste YOUR_KEY into the speedtest-ifttt.sh script (line 82) and run your script. It should work OOB.

I keep running into an issue with my key do I enter it in both fields listed secret key?

Send to IFTTT

secret_key="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}"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key}

I answered my own question updated both fields with my key however my main issue was I never name my applet on Maker Web Hooks..

Fantastic mod of speedtest-extras - question regarding output to Google Sheet:
The date/time appears in my Google Sheet as May 1, 2017 at 12:00am
Can the script be modified to add a leading zero to single digit dates so it inserts into Google Sheet as May 01, 2017 at 12:00am?

garycrj commented Jun 29, 2017

sudo ./speedtest-ifttt.sh
./speedtest-ifttt.sh: line 54: /usr/local/bin/speedtest-cli: No such file or directory
Congratulations! You've fired the speedtest event

Walkingman-UK commented Sep 27, 2017 edited

Hi
Just set this up and it works as it should (top Job from you).
Is there away to change the date time format to 27/09/2017 09:44:29
Thanks in advance

@garycrj perhaps you dont have speedtest-cli installed? sudo pip install speedtest-cli

I got it working, but I am getting a random decimal in front of some of my download readings. I havent been able to track it down to a particular pattern. Maybe its a certain server that is returning the data in a strange format?
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment