Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. import subprocess
  2. import datetime
  3. import time
  4. import re
  5. import sys
  6. toolbar_width = 40
  7. while True:
  8. try:
  9. output = subprocess.check_output(["ping", "-c 50", "-i 0.05", "google.ca"])
  10. except subprocess.CalledProcessError as e:
  11. if e.returncode == 2:
  12. # no responses received
  13. output = e.output
  14. else:
  15. print "error code " + str(e.returncode)
  16. print e.output
  17. if e.returncode == 68:
  18. time.sleep(2.5) # avoid flood of failed DNS requests
  19. continue
  20.  
  21. match = re.search("([0-9]?[0-9]?[0-9]\.[0-9])% packet loss", output)
  22. if match:
  23. data = "{0}, {1}%".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), match.group(1))
  24. loss = float(match.group(1))/100
  25. dots = int(round(loss * toolbar_width))
  26. spaces = toolbar_width - dots
  27. data = "{0},{1}[{2}{3}]".format(data, (" " * (5 - len(match.group(1)))), ("#" * dots), (" " * spaces))
  28.  
  29. if loss > 1.18:
  30. print data,
  31. print '\a'
  32. else:
  33. print data
  34.  
  35. with open("packet_loss_data.csv", "a") as outfile:
  36. outfile.write(data)
  37. outfile.write('\n')
  38. outfile.flush()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement