-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeedtest-ifttt.sh
executable file
·69 lines (58 loc) · 1.88 KB
/
speedtest-ifttt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/bash
##############################################################################
# Written by: Tyler Harris, 2018
# Run speedtest-cli within docker and append to Google Sheet through IFTTT
# - Docker Image: https://hub.docker.com/r/tianon/speedtest/
# - IFTTT Webhooks: https://ifttt.com/maker_webhooks
# - https://ifttt.com/applets/379108p-log-speedtest-results-to-spreadsheet
# - Sheet columns: "Date,Ping,Download (Mbps),Upload (Mbps)"
# License: GPL >=v3 [http://www.gnu.org/licenses/quick-guide-gplv3.html]
##############################################################################
# Character for separating values
# (commas are not safe, because some servers return speeds with commas)
delimiter=";"
# Temporary file holding speedtest-cli output
log=/tmp/speedtest-csv.log
SKIP_SPEEDTEST=false
# Prepare
start=`date +"%Y-%m-%d %H:%M:%S"`
mkdir -p `dirname $log`
if $SKIP_SPEEDTEST && test -f "$log"; then
# Reuse existing results (debugging)
1>&2 echo "** Reusing existing results: $log"
else
# Run Speedtest
echo "Running speed test..."
docker run --rm --net=host tianon/speedtest --secure --csv --csv-delimiter ";" --share > $log
fi
echo "Finished with results: `cat $log`"
# Parse
IFS="$delimiter" read -ra results <<< `cat $log`
server_id=${results[0]}
sponsor=${results[1]}
server_name=${results[2]}
timestamp=${results[3]}
distance=${results[4]}
server_ping=${results[5]}
download=${results[6]}
upload=${results[7]}
# Convert from bits to Mbps
download=`bc <<< "scale=2; $download / 1000 / 1000" `
upload=`bc <<< "scale=2; $upload / 1000 / 1000" `
# Send to IFTTT
echo "Sending to IFTTT..."
secret_key="<secret-key>"
curl https://maker.ifttt.com/trigger/speedtest/with/key/${secret_key} \
-X POST \
-H "Content-Type: application/json" \
-d @<(cat <<EOF
{
"value1":"${server_ping}",
"value2":"${download}",
"value3":"${upload}"
}
EOF
)
echo
rm -v "$log"
exit 0