Skip to content

Instantly share code, notes, and snippets.

@agrif
Created December 10, 2013 22:45
Show Gist options
  • Select an option

  • Save agrif/7901791 to your computer and use it in GitHub Desktop.

Select an option

Save agrif/7901791 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import time
try:
_, iface = sys.argv
except ValueError:
print("usage: {} [iface]".format(sys.argv[0]))
sys.exit(1)
def nice(i):
suffixes = ['B/s', 'kiB/s', 'MiB/s', 'GiB/s']
power = 0
while i > 1024 and power < len(suffixes) - 1:
i /= 1024
power += 1
return "{:.02f} {}".format(i, suffixes[power])
lasttx = None
lastrx = None
while True:
with open("/proc/net/dev") as f:
for l in f.readlines():
try:
name, rx, _, _, _, _, _, _, _, tx, _, _, _, _, _, _, _ = l.split()
if iface != name.rstrip(":"):
continue
except ValueError:
continue
tx = int(tx)
rx = int(rx)
if lasttx and lastrx:
print("{} in, {} out".format(*[nice(i) for i in [rx - lastrx, tx - lasttx]]))
lasttx = tx
lastrx = rx
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment