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

46 lines
1.3 KiB
Python
Raw Permalink Normal View History

2012-03-01 03:48:44 +01:00
# ping plugin by neersighted
import subprocess
import re
2012-03-29 10:12:41 +02:00
import os
2012-03-01 03:48:44 +01:00
2014-02-14 04:36:57 +01:00
from util import hook
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):
2013-09-04 12:30:04 +02:00
"""ping <host> [count] -- Pings <host> [count] times."""
2012-03-29 10:12:41 +02:00
if os.name == "nt":
return "Sorry, this command is not supported on Windows systems."
2014-02-14 04:30:38 +01:00
# TODO: Rewrite this entire command to work on Windows, somehow
2012-03-01 03:48:44 +01:00
args = inp.split(' ')
host = args[0]
2012-03-01 05:54:00 +01:00
2014-02-14 04:49:41 +01:00
# check for a second 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)
2013-08-01 14:04:41 +02:00
# I suck at regex, but this is causing issues, and I'm just going to remove it
# I assume it's no longer needed with the way we run the process
# host = re.sub(r'([^\s\w\.])+', '', host)
2012-03-01 05:54:00 +01:00
2013-09-05 04:11:18 +02:00
reply("Attempting to ping {} {} times...".format(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" \
2013-09-04 12:30:04 +02:00
% (m.group(1), m.group(3), m.group(2), m.group(4), count)