Skip to content

Instantly share code, notes, and snippets.

@bigntallmike
Created August 17, 2022 17:30
Show Gist options
  • Save bigntallmike/a9a12d8b995157a11c055306277bc0e9 to your computer and use it in GitHub Desktop.
Save bigntallmike/a9a12d8b995157a11c055306277bc0e9 to your computer and use it in GitHub Desktop.
Needed speed test data I could parse in a bash script with eval
#!/usr/bin/python3 -ttu
#
# Run speedtest-cli and extract data from csv in useful format for bash scripts
#
# Copyright (C) 2022 Michael T. Babcock -- Licensed LGPL-2.0-or-later
import csv
import subprocess
def speedtest_parse(data):
'''Grab parameters 6 and 7 (down and uplink) and convert to megabit integers'''
return [ int(float(_)/1000000) for _ in list(csv.reader([data]))[0][6:8] ]
def run_speedtest():
with subprocess.Popen(['speedtest-cli', '--csv'], stdout=subprocess.PIPE) as speedtest:
stdout = speedtest.stdout.read()
return stdout
if __name__ == '__main__':
speeds = speedtest_parse(run_speedtest().decode('UTF-8'))
print("SPEED_DOWNLINK=%d" % speeds[0])
print("SPEED_UPLINK=%d" % speeds[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment