This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/ping.py

36 lines
943 B
Python
Raw Normal View History

2012-03-01 03:48:44 +01:00
# ping plugin by neersighted
from util import hook
import subprocess
import re
2012-03-23 05:51:02 +01:00
PING_REGEX = "rtt min/avg/max/mdev = (\d+.\d+)/(\d+.\d+)/(\d+.\d+)/(\d+.\d+)"
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."
args = inp.split(' ')
host = args[0]
2012-03-01 05:54:00 +01:00
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-01 03:48:44 +01:00
if 'request timed out' in pingcmd or 'unknown host' in pingcmd:
return "error: could not ping host"
else:
2012-03-23 05:51:02 +01:00
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)