Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance JSON response parsing, prevent error leak #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions speedtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,30 @@ run_speedtest()
JSON=$(speedtest --accept-license --accept-gdpr -f json)
fi

DOWNLOAD="$(echo $JSON | jq -r '.download.bandwidth')"
UPLOAD="$(echo $JSON | jq -r '.upload.bandwidth')"
PING="$(echo $JSON | jq -r '.ping.latency')"
echo "Your download speed is $(($DOWNLOAD / 125000 )) Mbps ($DOWNLOAD Bytes/s)."
echo "Your upload speed is $(($UPLOAD / 125000 )) Mbps ($UPLOAD Bytes/s)."
echo "Your ping is $PING ms."
json_result_found="$(echo "$JSON" | jq 'select(.type == "result")')"

# Save results in the database
if $DB_SAVE;
then
echo "Saving values to database..."
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "download,host=$HOSTNAME value=$DOWNLOAD $DATE"
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "upload,host=$HOSTNAME value=$UPLOAD $DATE"
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "ping,host=$HOSTNAME value=$PING $DATE"
echo "Values saved."
if [ -n "${json_result_found}" ]; then
DOWNLOAD="$(echo "$JSON" | jq -r '.download.bandwidth')"
UPLOAD="$(echo "$JSON" | jq -r '.upload.bandwidth')"
PING="$(echo "$JSON" | jq -r '.ping.latency')"
echo "Your download speed is $((DOWNLOAD / 125000 )) Mbps ($DOWNLOAD Bytes/s)."
echo "Your upload speed is $((UPLOAD / 125000 )) Mbps ($UPLOAD Bytes/s)."
echo "Your ping is $PING ms."

# Save results in the database
if $DB_SAVE;
then
echo "Saving values to database..."
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "download,host=$HOSTNAME value=$DOWNLOAD $DATE"
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "upload,host=$HOSTNAME value=$UPLOAD $DATE"
curl -s -S -XPOST "$DB_HOST/write?db=$DB_NAME&precision=s&u=$DB_USERNAME&p=$DB_PASSWORD" \
--data-binary "ping,host=$HOSTNAME value=$PING $DATE"
echo "Values saved."
fi
else
echo "[error] Unable to retrieve JSON results from speedtest, please see previous log message"
fi
}

Expand Down