Skip to content

Instantly share code, notes, and snippets.

@zypeh
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zypeh/4705353b06f94e1ca367 to your computer and use it in GitHub Desktop.
Save zypeh/4705353b06f94e1ca367 to your computer and use it in GitHub Desktop.
Check network interface
#!/usr/bin/env python
import time
import sys
if len(sys.argv) > 1:
INTERFACE = sys.argv[1]
else:
INTERFACE = 'eth0'
STATS = []
print 'Interface:',INTERFACE
def rx():
ifstat = open('/proc/net/dev').readlines()
for interface in ifstat:
if INTERFACE in interface:
stat = float(interface.split(":")[1].split()[0])
STATS[0:] = [stat]
def tx():
ifstat = open('/proc/net/dev').readlines()
for interface in ifstat:
if INTERFACE in interface:
stat = float(interface.split()[9])
STATS[1:] = [stat]
if __name__ == '__main__':
try:
print 'In Out'
rx()
tx()
while True:
time.sleep(1)
rxstat_o = list(STATS)
rx()
tx()
RX = float(STATS[0])
RX_O = rxstat_o[0]
TX = float(STATS[1])
TX_O = rxstat_o[1]
RX_RATE = round((RX - RX_O)/1024,3)
TX_RATE = round((TX - TX_O)/1024,3)
print RX_RATE ,'KB ',TX_RATE ,'KB'
except KeyboardInterrupt, e:
print "\npyifstat exited"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment