From 90aa8976fcd6df50d97712c87c302128d34f04d2 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 23 Mar 2012 17:51:02 +1300 Subject: [PATCH] upgraded ping.py --- plugins/ping.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/plugins/ping.py b/plugins/ping.py index 310ace0..e2f6fe2 100755 --- a/plugins/ping.py +++ b/plugins/ping.py @@ -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) - return "min: %sms, max: %sms, average: %sms, range: %sms, count: %s"\ + 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) - -