Create a gist now

Instantly share code, notes, and snippets.

Embed
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}
@jscongdon

This comment has been minimized.

Show comment
Hide comment
@jscongdon

jscongdon Feb 3, 2016

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 😒

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

This comment has been minimized.

Show comment
Hide comment
@lubosm

lubosm Feb 25, 2016

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

lubosm commented Feb 25, 2016

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

@salt-lick

This comment has been minimized.

Show comment
Hide comment
@salt-lick

salt-lick Mar 9, 2016

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

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

@Landei

This comment has been minimized.

Show comment
Hide comment
@Landei

Landei 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.

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.

@salt-lick

This comment has been minimized.

Show comment
Hide comment
@salt-lick

salt-lick Mar 9, 2016

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

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

@meaverick

This comment has been minimized.

Show comment
Hide comment
@meaverick

meaverick Oct 13, 2016

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

meaverick commented Oct 13, 2016

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

This comment has been minimized.

Show comment
Hide comment
@drijd

drijd 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

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

@Stybyk

This comment has been minimized.

Show comment
Hide comment
@Stybyk

Stybyk 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 ?

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

This comment has been minimized.

Show comment
Hide comment
@babca

babca 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.

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.

@epifano83

This comment has been minimized.

Show comment
Hide comment
@epifano83

epifano83 Mar 19, 2017

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 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}

@epifano83

This comment has been minimized.

Show comment
Hide comment
@epifano83

epifano83 Mar 19, 2017

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..

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..

@esharksolutions

This comment has been minimized.

Show comment
Hide comment
@esharksolutions

esharksolutions May 20, 2017

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?

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

This comment has been minimized.

Show comment
Hide comment
@garycrj

garycrj 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

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

This comment has been minimized.

Show comment
Hide comment
@Walkingman-UK

Walkingman-UK Sep 27, 2017

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

Walkingman-UK commented Sep 27, 2017

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

@wesleycombs

This comment has been minimized.

Show comment
Hide comment
@wesleycombs

wesleycombs Oct 10, 2017

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

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

@wesleycombs

This comment has been minimized.

Show comment
Hide comment
@wesleycombs

wesleycombs Oct 10, 2017

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

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

@hornjt75862

This comment has been minimized.

Show comment
Hide comment
@hornjt75862

hornjt75862 Nov 11, 2017

I am trying to figure out how to find out what the "SECRET_KEY" value needs to be

I am trying to figure out how to find out what the "SECRET_KEY" value needs to be

@OhioJoe

This comment has been minimized.

Show comment
Hide comment
@OhioJoe

OhioJoe Nov 12, 2017

@hornjt75862, Secret_Key ->
log in to your IFTTT account: https://ifttt.com/
Click on your name (upper right hand corner of web page)
Click on Services
Click on Webhooks
Click on Settings (upper right hand corner of web page, under your name)
You should see : URL https://maker.ifttt.com/use/#####
The ##### part is your Secret Key!

Edit line #82(above -in the code)secret_key="SECRET_KEY"
Replace "SECRET_KEY" with "#####" what every was that part of the URL above.
DO NOT edit the last line #87.

Hope this helps

OhioJoe commented Nov 12, 2017

@hornjt75862, Secret_Key ->
log in to your IFTTT account: https://ifttt.com/
Click on your name (upper right hand corner of web page)
Click on Services
Click on Webhooks
Click on Settings (upper right hand corner of web page, under your name)
You should see : URL https://maker.ifttt.com/use/#####
The ##### part is your Secret Key!

Edit line #82(above -in the code)secret_key="SECRET_KEY"
Replace "SECRET_KEY" with "#####" what every was that part of the URL above.
DO NOT edit the last line #87.

Hope this helps

@OhioJoe

This comment has been minimized.

Show comment
Hide comment
@OhioJoe

OhioJoe Nov 13, 2017

Your secret key is generated when you connect the IFTTT maker channel, and is shown here, https://ifttt.com/maker

OhioJoe commented Nov 13, 2017

Your secret key is generated when you connect the IFTTT maker channel, and is shown here, https://ifttt.com/maker

@AndrewLindsay

This comment has been minimized.

Show comment
Hide comment
@AndrewLindsay

AndrewLindsay Nov 17, 2017

I’ve got everything running (apparently) I get the message ‘Congratulations! You’ve fired the speedtest event’ but I don’t get a spreadsheet created in my google sheets.

Any pointers on what I have to do to get this link working?

I’ve got everything running (apparently) I get the message ‘Congratulations! You’ve fired the speedtest event’ but I don’t get a spreadsheet created in my google sheets.

Any pointers on what I have to do to get this link working?

@teshiburu

This comment has been minimized.

Show comment
Hide comment
@teshiburu

teshiburu Nov 30, 2017

Hi - i was wondering if there was a way to get this to pass a 4th value to the IFTTT channel.

I added the relevant values here (value4)

value1=echo $server_ping | cut -d" " -f1
value2=echo $download | cut -d" " -f1
value3=echo $upload | cut -d" " -f1
value4=echo $server | cut -d" " -f1
json="{"value1":"${value1}","value2":"${value2}","value3":"${value3}","value4":"${value4}"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key}

But when i go to the IFTTT applet and att |||{{value4}} to the applet it says there is an error and wont let me save :(

Hi - i was wondering if there was a way to get this to pass a 4th value to the IFTTT channel.

I added the relevant values here (value4)

value1=echo $server_ping | cut -d" " -f1
value2=echo $download | cut -d" " -f1
value3=echo $upload | cut -d" " -f1
value4=echo $server | cut -d" " -f1
json="{"value1":"${value1}","value2":"${value2}","value3":"${value3}","value4":"${value4}"}"
curl -X POST -H "Content-Type: application/json" -d "${json}" https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key}

But when i go to the IFTTT applet and att |||{{value4}} to the applet it says there is an error and wont let me save :(

@kokest

This comment has been minimized.

Show comment
Hide comment
@kokest

kokest Dec 14, 2017

i keep getting the following error : speedtest-ifttt.sh: line 87: curl: command not found
i havent changed anything on line 87. any ideas?

kokest commented Dec 14, 2017

i keep getting the following error : speedtest-ifttt.sh: line 87: curl: command not found
i havent changed anything on line 87. any ideas?

@mrminus

This comment has been minimized.

Show comment
Hide comment
@mrminus

mrminus Dec 30, 2017

I have this working and posting to Google Sheets via IFTTT without any issues. I do have a few questions that maybe the author or another user can opine on:

  1. The standard ingredient for date/time is in UCT and has that annoying "at" in it. I can convert it in my sheet to my local time (because, if I want to know when something went below my threshold I really do need it in my local time...). How can I just have the IFTTT ingredient give me US/PST?
  2. I added my own field for tracking the ongoing percentage delta test to test. I do that via the calculation of : (currentDL - previousDL) / previousDL, and formatting the cell as a percent. I included a screen shot. Since the script appends to the google sheet in the next row, is there any way, and this is a Sheets question mostly, to pre-populate the formula to the cell so that when the applet does the append the calculation is already there? right now i have to do a drag/copy every few hours.
    internet-tracking

by the way, this is how i'm calculating the local time from UCT.

=(datevalue(regexextract(A2, "^(.+) at ")) + time(regexextract(A2, "(?i) at (\d+):") + 12 * (right(A2, 2) = "PM") * (regexextract(A2, "(?i) at (\d+):") <> "12") - 12 * (right(A2, 2) = "AM") * (regexextract(A2, "(?i) at (\d+):") = "12"), regexextract(A2, "(?i) at .+:(\d*)[ap]m$"), 0))-time(8,0,0)

mrminus commented Dec 30, 2017

I have this working and posting to Google Sheets via IFTTT without any issues. I do have a few questions that maybe the author or another user can opine on:

  1. The standard ingredient for date/time is in UCT and has that annoying "at" in it. I can convert it in my sheet to my local time (because, if I want to know when something went below my threshold I really do need it in my local time...). How can I just have the IFTTT ingredient give me US/PST?
  2. I added my own field for tracking the ongoing percentage delta test to test. I do that via the calculation of : (currentDL - previousDL) / previousDL, and formatting the cell as a percent. I included a screen shot. Since the script appends to the google sheet in the next row, is there any way, and this is a Sheets question mostly, to pre-populate the formula to the cell so that when the applet does the append the calculation is already there? right now i have to do a drag/copy every few hours.
    internet-tracking

by the way, this is how i'm calculating the local time from UCT.

=(datevalue(regexextract(A2, "^(.+) at ")) + time(regexextract(A2, "(?i) at (\d+):") + 12 * (right(A2, 2) = "PM") * (regexextract(A2, "(?i) at (\d+):") <> "12") - 12 * (right(A2, 2) = "AM") * (regexextract(A2, "(?i) at (\d+):") = "12"), regexextract(A2, "(?i) at .+:(\d*)[ap]m$"), 0))-time(8,0,0)

@pbouianov

This comment has been minimized.

Show comment
Hide comment
@pbouianov

pbouianov Jan 1, 2018

Not sure about #1 as for me that ingredient comes w/ local time (though I do have that annoying string format), but your #2 custom field can be done w/ something like

=(INDIRECT(CONCAT("E", ROW()))-INDIRECT(CONCAT("E", ROW()-1)))/INDIRECT(CONCAT("E", ROW()-1))

Just change the colname to your download speed col.

pbouianov commented Jan 1, 2018

Not sure about #1 as for me that ingredient comes w/ local time (though I do have that annoying string format), but your #2 custom field can be done w/ something like

=(INDIRECT(CONCAT("E", ROW()))-INDIRECT(CONCAT("E", ROW()-1)))/INDIRECT(CONCAT("E", ROW()-1))

Just change the colname to your download speed col.

@shanep2300

This comment has been minimized.

Show comment
Hide comment
@shanep2300

shanep2300 Feb 27, 2018

Is there any update to the script or where it pulls the speeds from? At my old apartment I was paying for 60Mb/s down and my sheets were correctly averaging around 60. Now that I'm at a new location and new house, I'm paying for 100Mb/s down and it's averaging at 70. When I use speedtest.net, it shows 70Mb/s. When I use speedof.me it shows 100Mb/s+. Bandwidthplace.com shows around 70. And testmy.net shows 100Mb/s+. So, which is it!?

Is there any update to the script or where it pulls the speeds from? At my old apartment I was paying for 60Mb/s down and my sheets were correctly averaging around 60. Now that I'm at a new location and new house, I'm paying for 100Mb/s down and it's averaging at 70. When I use speedtest.net, it shows 70Mb/s. When I use speedof.me it shows 100Mb/s+. Bandwidthplace.com shows around 70. And testmy.net shows 100Mb/s+. So, which is it!?

@HughP

This comment has been minimized.

Show comment
Hide comment
@HughP

HughP Mar 17, 2018

Is there a way to record the mac address of the triggering machine?

HughP commented Mar 17, 2018

Is there a way to record the mac address of the triggering machine?

@davedavis

This comment has been minimized.

Show comment
Hide comment
@davedavis

davedavis Apr 9, 2018

@mrminus Did you ever figure that out? Could you share?

@mrminus Did you ever figure that out? Could you share?

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