Created
November 25, 2012 19:40
-
-
Save faried/4144973 to your computer and use it in GitHub Desktop.
my horrible old network stats code
This file contains hidden or 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 python | |
import os | |
import sys | |
import time | |
__author__ = 'Faried Nawaz' | |
__version_ = '20030625' | |
def usage(str=None): | |
if str: | |
sys.stderr.write(str + '\n'); | |
sys.stderr.write('usage: istats.py interface-name\n') | |
def doit(interface): | |
"""Do the work.""" | |
cmd = '/sbin/ifconfig %s' % interface | |
addr = '' | |
basei = 0 | |
baseo = 0 | |
p = os.popen(cmd) | |
lines = p.readlines() | |
p.close() | |
ltemp = lines[7].split() | |
basei = long(ltemp[1].replace('bytes:', '')) | |
baseo = long(ltemp[5].replace('bytes:', '')) | |
print '''%-4s %9s %9s | |
%-4s %9ld %9ld''' % ('Name', 'In', 'Out', | |
interface, basei, baseo) | |
while 1: | |
time.sleep(1) | |
p = os.popen(cmd) | |
lines = p.readlines() | |
p.close() | |
ltemp = lines[7].split() | |
ipkts = long(ltemp[1].replace('bytes:', '')) - basei | |
opkts = long(ltemp[5].replace('bytes:', '')) - baseo | |
basei = long(ltemp[1].replace('bytes:', '')) | |
baseo = long(ltemp[5].replace('bytes:', '')) | |
print '%-4s %9ld %9ld' % (interface, ipkts, opkts) | |
def main(args): | |
"""Main branching logic.""" | |
if len(args) == 0: | |
usage() | |
sys.exit(1) | |
# check if the interface exists | |
p = os.popen('ifconfig %s 2>&1' % args[0]); | |
line = p.readline() | |
p.close() | |
if line.startswith('ifconfig: '): | |
usage('no such interface: ' + args[0]) | |
sys.exit(1) | |
else: | |
doit(args[0]) | |
if __name__ == '__main__': main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment