Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import subprocess
- import json
- def format_bytes(size):
- # 2**10 = 1024
- power = 2**10
- n = 0
- power_labels = {0 : '', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
- while size > power:
- size /= power
- n += 1
- return size, power_labels[n]+'B'
- output = subprocess.getstatusoutput("vnstat -tr 2 --json -i en0")
- result = json.loads(output[1])
- rxb = result['rx']['bytespersecond']
- txb = result['tx']['bytespersecond']
- formatted_rx = format_bytes(rxb)
- formatted_tx = format_bytes(txb)
- rx = round(formatted_rx[0], 2)
- tx = round(formatted_tx[0], 2)
- print("{}{}/s {}{}/s".format(rx, formatted_rx[1], tx, formatted_tx[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement