Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. import re
  2. import subprocess
  3.  
  4.  
  5. def ping(host):
  6. """ Ping the address/hostname and return True if packet loss is less than
  7. 60%. All other results return False or print and error."""
  8. exp = re.compile(r"\s(\d{1,3})\%\s")
  9. try:
  10. test = subprocess.Popen(["ping", "-c 5", "-W 2", host],
  11. stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  12. out, error = test.communicate()
  13. if out:
  14. stats = re.search(exp, out)
  15. loss = int(stats.group(1))
  16.  
  17. return loss <= 60
  18.  
  19. else:
  20. return False
  21.  
  22. except subprocess.CalledProcessError:
  23. return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement