You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ping.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import subprocess
  2. from i3pystatus import IntervalModule
  3. class Ping(IntervalModule):
  4. interval = 2
  5. settings = (
  6. ("host", "host to ping"),
  7. ("format", "format string"),
  8. ("format_down", "format down string"),
  9. ("color", "color"),
  10. ("color_down", "color down")
  11. )
  12. host = "8.8.8.8"
  13. format = "{ping} ms"
  14. format_down = "down"
  15. color = "#FFFFFF"
  16. color_down = "#FF0000"
  17. def ping_host(self):
  18. p = subprocess.Popen(["ping", "-c1", "-w%d" % self.interval,
  19. self.host], stdout=subprocess.PIPE,
  20. stderr=subprocess.DEVNULL)
  21. out, _ = p.communicate()
  22. if p.returncode == 0:
  23. return out.decode().split("\n")[1].split("time=")[1].split()[0]
  24. else:
  25. return ""
  26. def run(self):
  27. ping = self.ping_host()
  28. if not ping:
  29. self.output = {
  30. "full_text": self.format_down,
  31. "color": self.color_down
  32. }
  33. return
  34. ping = float(ping)
  35. self.output = {
  36. "full_text": self.format.format(ping=ping),
  37. "color": self.color
  38. }
  39. if __name__ == "__main__":
  40. print(Ping().ping_host())