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.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. ("format_pause", "format pause string"),
  10. ("color", "color"),
  11. ("color_down", "color down"),
  12. ("color_pause", "color pause")
  13. )
  14. active = True
  15. color = "#FFFFFF"
  16. color_down = "#FF0000"
  17. color_pause = "#FF4500"
  18. format = "{ping} ms"
  19. format_down = "{ping} ms"
  20. format_pause = "{ping} ms"
  21. host = "8.8.8.8"
  22. on_leftclick = "switch_state"
  23. def ping_host(self):
  24. p = subprocess.Popen(["ping", "-c1", "-w%d" % self.interval,
  25. self.host], stdout=subprocess.PIPE,
  26. stderr=subprocess.DEVNULL)
  27. out, _ = p.communicate()
  28. if p.returncode == 0:
  29. return out.decode().split("\n")[1].split("time=")[1].split()[0]
  30. else:
  31. return ""
  32. def run(self):
  33. if not self.active:
  34. self.output = {
  35. "full_text": self.format_pause.format(ping=0),
  36. "color": self.color_pause
  37. }
  38. return
  39. ping = self.ping_host()
  40. if not ping:
  41. self.output = {
  42. "full_text": self.format_down,
  43. "color": self.color_down
  44. }
  45. return
  46. ping = float(ping)
  47. self.output = {
  48. "full_text": self.format.format(ping=ping),
  49. "color": self.color
  50. }
  51. def switch_state(self):
  52. self.active = not self.active