Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import subprocess
  3. import json
  4.  
  5. def format_bytes(size):
  6.     # 2**10 = 1024
  7.     power = 2**10
  8.     n = 0
  9.     power_labels = {0 : '', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
  10.     while size > power:
  11.         size /= power
  12.         n += 1
  13.     return size, power_labels[n]+'B'
  14.  
  15. output = subprocess.getstatusoutput("vnstat -tr 2 --json -i en0")
  16.  
  17. result = json.loads(output[1])
  18. rxb = result['rx']['bytespersecond']
  19. txb = result['tx']['bytespersecond']
  20.  
  21. formatted_rx = format_bytes(rxb)
  22. formatted_tx = format_bytes(txb)
  23.  
  24. rx = round(formatted_rx[0], 2)
  25. tx = round(formatted_tx[0], 2)
  26.  
  27. print("{}{}/s {}{}/s".format(rx, formatted_rx[1], tx, formatted_tx[1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement