0

There is this script for testing the internet speed directly from the command line:

curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -

Retrieving speedtest.net configuration...
Testing from XXXX (X.X.X.5)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by XXXX (XX) [0.54 km]: 3.513 ms
Testing download speed................................................................................
Download: 191.68 Mbit/s
Testing upload speed................................................................................................
Upload: 154.36 Mbit/s

I know I can get either the Download or Upload line using | grep Download/Upload

But how can I cast both Download and Upload into a variables?

5
  • Capture the result, and you can search through it more than once. Commented Apr 5, 2018 at 22:02
  • Ask the script for a machine processable format. I see it has a --csv option. Commented Apr 5, 2018 at 22:04
  • BTW, you're putting a lot of trust into sivel not to let their github credentials be leaked, in github not to have a security breach, etc. Much safer to cache speedtest.py locally than to download a new version every time you run it, which (if you're cronning that invocation) means that any transient exploit will hit you. (If said maintainer GPG-signs their release tags, then you can verify that a git commit really came from them, subject to an appropriate level of confidence around private key storage). Commented Apr 5, 2018 at 22:07
  • @CharlesDuffy How would I even curl speedtest.py locally? If not curl, what can I use to run it? Commented Apr 5, 2018 at 22:15
  • result=$(python /path/to/speedtest.py). curl is a download tool. You don't need it at all if the script is already downloaded. Commented Apr 5, 2018 at 22:17

2 Answers 2

1

In one shot:

mapfile -t speeds < <(
    curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | 
    python - | 
    grep -oP '(Up|Down)load: \K[\d.]+'
)
echo "Down: ${speeds[0]}"
echo "Up: ${speeds[1]}"
Sign up to request clarification or add additional context in comments.

Comments

0
# save your value *once*
result=$(curl ...)

# ...then you can search through it multiple times.
if [[ $result =~ 'Download: '([[:digit:].]+)' Mbit' ]]; then 
    download_speed=${BASH_REMATCH[1]}
fi
if [[ $result =~ 'Upload: '([[:digit:].]+)' Mbit' ]]; then 
    upload_speed=${BASH_REMATCH[1]}
fi

echo "Test results: Download speed ${download_speed:-unknown}"
echo "              Upload speed ${upload_speed:-unknown}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.