To preface this: the last time I wrote "code" was about 20 years ago. It was some QBasic script in school. My ISP heavily throttles my Netflix traffic and is all around kind of douchey, I don't get the full bandwidth (I know it's "best effort", but still). So I bought a RPi3, went to the Google and wildly copied code from other people.
Now I have a Python script (that should be well known) speedtest.py, and I got a bash script, that extracts the relevant things and writes it to a MySQL Database.
# temp file holding output to snip it
user=$USER
if test -z $user; then
user=$USERNAME
fi
log=/tmp/$user/speedtest-mysql.log
# Local functions
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
}
mkdir -p `dirname $log`
# run speedtest
/usr/local/bin/speedtest --share > $log
# parse the output
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: "`
#create timestamp for database
timestamp=`date +"%Y-%m-%d %H:%M:%S"`
# Send to MySQL
value1=`echo $server_ping | cut -d" " -f1`
value2=`echo $download | cut -d" " -f1`
value3=`echo $upload | cut -d" " -f1`
sql="INSERT INTO $db_table (timestamp,ping,download,upload,server,share_url) VALUES ('$timestamp','$server_ping','$download','$upload','$server','$share_url');"
echo "$sql" | mysql -u$db_user -p$db_passwd -h$db_host $db_name
Problem now is: sometimes I get an output from the speedtest with 95.53 MBit/s Download and 9.93 MBit/s Upload, but my Database gets filled with an upload value of 0.90. The relevant output of speedtest.py that is written to the logfile:
Retrieving speedtest.net configuration...
Testing from UPC Austria (80.110.109.207)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Timewarp IT Consulting GmbH (Vienna) [2.79 km]: 32.403 ms
Testing download speed................................................................................
Download: 17.46 Mbit/s
Testing upload speed................................................................................................
Upload: 2.28 Mbit/s
Share results: http://www.speedtest.net/result/7075502570.png
as said: My last voyage to coding was back in the 90s, and my google-fu is failing to find me an answer to this problem. Can somebody help?
/usr/local/bin/speedtestyou just wrote a hardcoded string, then someone who didn't havespeedtestinstalled could run the script themselves to see the problem, and thus be able to test their (or someone else's) proposed fix.