Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import platform
- import os
- import time
- def fancyTable(itemDict):
- leftWidth = 15
- rightWidth = 15
- print('Interface Statistics'.center(leftWidth + rightWidth, '-'))
- for k, v in itemDict.items():
- print(str(k.ljust(leftWidth)) + str(v).rjust(rightWidth))
- def Linux():
- print("Linux")
- def Windows():
- traffic = str(os.popen('netstat -e').read())
- oldTraffic = {'RX packets': int(traffic[142:162]) + int(traffic[198:214]),
- 'RX bytes': int(traffic[80:110]),
- 'RX discards': int(traffic[239:266]),
- 'RX errors': int(traffic[289:318]),
- 'RX unknown': int(traffic[352:370]),
- 'TX packets': int(traffic[162:178]) + int(traffic[214:230]),
- 'TX bytes': int(traffic[111:127]),
- 'TX discards': int(traffic[267:283]),
- 'TX errors': int(traffic[319:335])}
- monitorTime = 0
- command = ""
- while 1:
- print("Type 'show' to see interface statistics or type 'monitor' to see actual traffic on the interface.")
- command = input()
- if (command=="show"):
- fancyTable(oldTraffic)
- break
- elif (command=="monitor"):
- while True:
- print("Time: " + str(monitorTime) + " seconds")
- traffic = str(os.popen('netstat -e').read())
- newTraffic = {'RX packets': int(traffic[142:162]) + int(traffic[198:214]) - oldTraffic['RX packets'],
- 'RX bytes': int(traffic[80:110]) - oldTraffic['RX bytes'],
- 'RX discards': int(traffic[239:266]) - oldTraffic['RX discards'],
- 'RX errors': int(traffic[289:318]) - oldTraffic['RX errors'],
- 'RX unknown': int(traffic[352:370]) - oldTraffic['RX unknown'],
- 'TX packets': int(traffic[162:178]) + int(traffic[214:230]) - oldTraffic['TX packets'],
- 'TX bytes': int(traffic[111:127]) - oldTraffic['TX bytes'],
- 'TX discards': int(traffic[267:283]) - oldTraffic['TX discards'],
- 'TX errors': int(traffic[319:335]) - oldTraffic['TX errors']}
- fancyTable(newTraffic)
- monitorTime += 10
- print()
- time.sleep(10)
- else: continue
- def main():
- identifySystem = platform.uname()[0]
- if identifySystem == "Linux:": Linux()
- if identifySystem == "Windows": Windows()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement