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

39 lines
1,007 B
Python
Executable file

# ping plugin by neersighted
from util import hook
import subprocess
import re
@hook.command
def ping(inp, reply=None):
".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)
host = re.sub(r'([^\s\w\.])+', '', host)
reply("Attempting to ping %s %s times..." % (host, 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: %sms, max: %sms, average: %sms, range: %sms, count: %s"\
% (m.group(1), m.group(3), m.group(2), m.group(4), count)