added ping plugin
This commit is contained in:
parent
8bb4edc07e
commit
08de70d9e0
32
plugins/ping.py
Normal file
32
plugins/ping.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
# ping plugin by neersighted
|
||||
from util import hook
|
||||
import subprocess
|
||||
import re
|
||||
|
||||
|
||||
@hook.command
|
||||
def ping(inp):
|
||||
".ping <host> [count] -- Pings <host> [count] times."
|
||||
|
||||
args = inp.split(' ')
|
||||
host = args[0]
|
||||
|
||||
if len(args) > 1:
|
||||
count = args[1]
|
||||
count = int(count)
|
||||
if count > 20:
|
||||
count = 20
|
||||
else:
|
||||
count = 5
|
||||
|
||||
count = str(count)
|
||||
|
||||
pingcmd = subprocess.check_output("ping -c "\
|
||||
+ count + " " + host, shell=True)
|
||||
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: %s, max: %s, average: %s, range: %s, count: %s"\
|
||||
% (m.group(1), m.group(2), m.group(3), m.group(4), count)
|
Reference in a new issue