Advertisement
Isaacmm

Untitled

May 26th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import platform
  3. import os
  4. import time
  5.  
  6. def fancyTable(itemDict):
  7. leftWidth = 15
  8. rightWidth = 15
  9. print('Interface Statistics'.center(leftWidth + rightWidth, '-'))
  10. for k, v in itemDict.items():
  11. print(str(k.ljust(leftWidth)) + str(v).rjust(rightWidth))
  12.  
  13. def Linux():
  14. print("Linux")
  15.  
  16. def Windows():
  17. traffic = str(os.popen('netstat -e').read())
  18. oldTraffic = {'RX packets': int(traffic[142:162]) + int(traffic[198:214]),
  19. 'RX bytes': int(traffic[80:110]),
  20. 'RX discards': int(traffic[239:266]),
  21. 'RX errors': int(traffic[289:318]),
  22. 'RX unknown': int(traffic[352:370]),
  23. 'TX packets': int(traffic[162:178]) + int(traffic[214:230]),
  24. 'TX bytes': int(traffic[111:127]),
  25. 'TX discards': int(traffic[267:283]),
  26. 'TX errors': int(traffic[319:335])}
  27.  
  28. monitorTime = 0
  29. command = ""
  30. while 1:
  31. print("Type 'show' to see interface statistics or type 'monitor' to see actual traffic on the interface.")
  32. command = input()
  33. if (command=="show"):
  34. fancyTable(oldTraffic)
  35. break
  36. elif (command=="monitor"):
  37. while True:
  38. print("Time: " + str(monitorTime) + " seconds")
  39. traffic = str(os.popen('netstat -e').read())
  40. newTraffic = {'RX packets': int(traffic[142:162]) + int(traffic[198:214]) - oldTraffic['RX packets'],
  41. 'RX bytes': int(traffic[80:110]) - oldTraffic['RX bytes'],
  42. 'RX discards': int(traffic[239:266]) - oldTraffic['RX discards'],
  43. 'RX errors': int(traffic[289:318]) - oldTraffic['RX errors'],
  44. 'RX unknown': int(traffic[352:370]) - oldTraffic['RX unknown'],
  45. 'TX packets': int(traffic[162:178]) + int(traffic[214:230]) - oldTraffic['TX packets'],
  46. 'TX bytes': int(traffic[111:127]) - oldTraffic['TX bytes'],
  47. 'TX discards': int(traffic[267:283]) - oldTraffic['TX discards'],
  48. 'TX errors': int(traffic[319:335]) - oldTraffic['TX errors']}
  49. fancyTable(newTraffic)
  50. monitorTime += 10
  51. print()
  52. time.sleep(10)
  53. else: continue
  54.  
  55.  
  56. def main():
  57. identifySystem = platform.uname()[0]
  58. if identifySystem == "Linux:": Linux()
  59. if identifySystem == "Windows": Windows()
  60.  
  61. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement