2012-03-01 03:48:44 +01:00
|
|
|
# ping plugin by neersighted
|
|
|
|
from util import hook
|
|
|
|
import subprocess
|
|
|
|
import re
|
2012-03-29 10:12:41 +02:00
|
|
|
import os
|
2012-03-01 03:48:44 +01:00
|
|
|
|
2012-03-23 06:14:58 +01:00
|
|
|
ping_regex = re.compile(r"(\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)")
|
2012-03-23 05:51:02 +01:00
|
|
|
|
2012-03-01 03:48:44 +01:00
|
|
|
|
|
|
|
@hook.command
|
2012-03-01 04:12:30 +01:00
|
|
|
def ping(inp, reply=None):
|
2012-03-01 03:48:44 +01:00
|
|
|
".ping <host> [count] -- Pings <host> [count] times."
|
2012-03-31 00:04:11 +02:00
|
|
|
|
2012-03-29 10:12:41 +02:00
|
|
|
if os.name == "nt":
|
2012-03-31 00:04:11 +02:00
|
|
|
return "Sorry, this command is not supported on Windows systems."
|
2012-03-01 03:48:44 +01:00
|
|
|
|
|
|
|
args = inp.split(' ')
|
|
|
|
host = args[0]
|
2012-03-01 05:54:00 +01:00
|
|
|
|
2012-03-29 10:12:41 +02:00
|
|
|
# check for a seccond argument and set the ping count
|
2012-03-01 03:48:44 +01:00
|
|
|
if len(args) > 1:
|
2012-03-23 05:51:02 +01:00
|
|
|
count = int(args[1])
|
2012-03-01 03:48:44 +01:00
|
|
|
if count > 20:
|
|
|
|
count = 20
|
|
|
|
else:
|
|
|
|
count = 5
|
|
|
|
|
|
|
|
count = str(count)
|
|
|
|
|
2012-03-01 05:54:00 +01:00
|
|
|
host = re.sub(r'([^\s\w\.])+', '', host)
|
|
|
|
|
2012-03-01 04:12:30 +01:00
|
|
|
reply("Attempting to ping %s %s times..." % (host, count))
|
2012-03-01 05:54:00 +01:00
|
|
|
|
2012-03-23 05:51:02 +01:00
|
|
|
pingcmd = subprocess.check_output(["ping", "-c", count, host])
|
2012-03-23 06:14:58 +01:00
|
|
|
if "request timed out" in pingcmd or "unknown host" in pingcmd:
|
2012-03-01 03:48:44 +01:00
|
|
|
return "error: could not ping host"
|
|
|
|
else:
|
2012-03-23 06:14:58 +01:00
|
|
|
m = re.search(ping_regex, pingcmd)
|
2012-03-23 05:51:02 +01:00
|
|
|
return "min: %sms, max: %sms, average: %sms, range: %sms, count: %s" \
|
2012-03-01 05:01:11 +01:00
|
|
|
% (m.group(1), m.group(3), m.group(2), m.group(4), count)
|