upgraded ping.py

This commit is contained in:
Luke Rogers 2012-03-23 17:51:02 +13:00
parent 954ea57fce
commit 90aa8976fc

View file

@ -3,6 +3,8 @@ from util import hook
import subprocess
import re
PING_REGEX = "rtt min/avg/max/mdev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)"
@hook.command
def ping(inp, reply=None):
@ -12,8 +14,7 @@ def ping(inp, reply=None):
host = args[0]
if len(args) > 1:
count = args[1]
count = int(count)
count = int(args[1])
if count > 20:
count = 20
else:
@ -25,14 +26,10 @@ def ping(inp, reply=None):
reply("Attempting to ping %s %s times..." % (host, count))
pingcmd = subprocess.check_output("ping -c "\
+ count + " " + host, shell=True)
pingcmd = subprocess.check_output(["ping", "-c", count, host])
if 'request timed out' in pingcmd or 'unknown host' in pingcmd:
return "error: could not ping host"
else:
m = re.search(r"rtt min/avg/max/mdev = "\
"(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)", pingcmd)
m = re.search(PING_REGEX, pingcmd)
return "min: %sms, max: %sms, average: %sms, range: %sms, count: %s" \
% (m.group(1), m.group(3), m.group(2), m.group(4), count)