upgraded ping.py
This commit is contained in:
parent
954ea57fce
commit
90aa8976fc
1 changed files with 6 additions and 9 deletions
|
@ -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)
|
||||
|
||||
|
||||
|
|
Reference in a new issue