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