#!/usr/bin/python
+import subprocess
+
class Item(object):
def __init__(self, **args):
self.args = args
for key in d.keys():
ret[str(key)] = d[key]
return ret
+
+def ping(host, count=5, wait=10):
+ cmd = subprocess.Popen(
+ ["ping", "-c%d" % count, "-w%d" % wait, host],
+ stdout = subprocess.PIPE,
+ stderr = subprocess.PIPE,
+ )
+
+ latency = None
+
+ out, error = cmd.communicate()
+
+ for line in out.split("\n"):
+ if not line.startswith("rtt"):
+ continue
+
+ line = line.split()
+ if len(line) < 4:
+ break
+
+ rtts = line[3].split("/")
+ if len(rtts) < 4:
+ break
+
+ latency = "%.1f" % float(rtts[1])
+
+ return latency
+
+
+if __name__ == "__main__":
+ print ping("www.ipfire.org")
+ print ping("www.rowie.at")