Created
November 1, 2014 06:00
-
-
Save jayeye/a705611dfa94cec7d02b to your computer and use it in GitHub Desktop.
Quick-and-dirty program to show network traffic rate on a mac, updating once a second.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
from __future__ import print_function | |
import subprocess | |
import time | |
def netstat(): | |
ifs0 = subprocess.check_output(['/usr/sbin/netstat', '-b', '-i']) | |
retval = dict() | |
for i in ifs0.splitlines(): | |
fields = i.split() | |
if fields[0].startswith('en') or fields[0].startswith('utun'): | |
if fields[0] not in retval: | |
retval[fields[0]] = map(int, (fields[-7], fields[-5], fields[-4], fields[-2])) | |
return retval | |
time0 = time.time() | |
ns0 = netstat() | |
print(ns0) | |
while True: | |
time.sleep(.99) | |
time1 = time.time() | |
delta_t = time1 - time0 | |
ns1 = netstat() | |
for ifname in sorted(ns1.keys()): | |
if ifname not in ns0.keys(): | |
continue | |
f0 = ns0[ifname] | |
f1 = ns1[ifname] | |
if f0 == f1: | |
continue | |
print('%-6s %.3g kipps %.3g Mibps %.3g kopps %.3g Mobps' % (ifname, | |
(f1[0] - f0[0]) / (1024.0 * delta_t), | |
(f1[1] - f0[1]) / (131072 * delta_t), | |
(f1[2] - f0[2]) / (1024.0 * delta_t), | |
(f1[3] - f0[3]) / (131072 * delta_t))) | |
print() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment