Skip to content

Instantly share code, notes, and snippets.

@sebble
Last active February 6, 2016 11:32
Show Gist options
  • Save sebble/b33e485feeacccee55b2 to your computer and use it in GitHub Desktop.
Save sebble/b33e485feeacccee55b2 to your computer and use it in GitHub Desktop.
Speed Tests
#!/usr/bin/env python
from commands import getstatusoutput
from datetime import datetime
from re import findall
REPEAT = 3
for i in range(REPEAT):
now = datetime.now()
OUTPUT_CSV = now.strftime('/var/log/speedtest/speedtest_%Y%m.log')
## run speedtest
results = {'datetime': now.isoformat()}
output = getstatusoutput('speedtest')[1]
results.update({'datetime_end': datetime.now().isoformat()})
## parse results
results.update( dict( zip( ('provider', 'external_ip'), findall( 'Testing from ([^(]+) \(([0-9\.]+)\)\.\.\.', output )[0] ) ) )
results.update( dict( zip( ('remote_host', 'ping_ms'), findall( 'Hosted by ([^:]+): ([0-9\.]+) ms', output )[0] ) ) )
results.update( { 'download_Mbit_s': findall( 'Download: ([0-9\.]+) Mbit/s', output )[0] } )
results.update( { 'upload_Mbit_s': findall( 'Upload: ([0-9\.]+) Mbit/s', output )[0] } )
## write CSV
header_order = ('datetime', 'datetime_end', 'provider', 'external_ip',
'remote_host', 'ping_ms', 'download_Mbit_s', 'upload_Mbit_s')
with open(OUTPUT_CSV, 'a+') as log:
log.write(','.join([results[h] for h in header_order])+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment