import subprocess from i3pystatus import IntervalModule class Ping(IntervalModule): interval = 2 settings = ( ("host", "host to ping"), ("format", "format string"), ("format_down", "format down string"), ("color", "color"), ("color_down", "color down") ) host = "8.8.8.8" format = "{ping} ms" format_down = "down" color = "#FFFFFF" color_down = "#FF0000" def ping_host(self): p = subprocess.Popen(["ping", "-c1", "-w%d" % self.interval, self.host], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL) out, _ = p.communicate() if p.returncode == 0: return out.decode().split("\n")[1].split("time=")[1].split()[0] else: return "" def run(self): ping = self.ping_host() if not ping: self.output = { "full_text": self.format_down, "color": self.color_down } return ping = float(ping) self.output = { "full_text": self.format.format(ping=ping), "color": self.color } if __name__ == "__main__": print(Ping().ping_host())