diff --git a/plugins/Unix/config.py b/plugins/Unix/config.py index 34096c024..330ec1be3 100644 --- a/plugins/Unix/config.py +++ b/plugins/Unix/config.py @@ -85,5 +85,9 @@ conf.registerGlobalValue(Unix.wtf, 'command', registry.String(utils.findBinaryInPath('wtf') or '', """Determines what command will be called for the wtf command.""")) +conf.registerGroup(Unix, 'ping') +conf.registerGlobalValue(Unix.ping, 'command', + registry.String(utils.findBinaryInPath('ping') or '', """Determines what + command will be called for the ping command.""")) # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/Unix/plugin.py b/plugins/Unix/plugin.py index d4da8e070..3898f150f 100644 --- a/plugins/Unix/plugin.py +++ b/plugins/Unix/plugin.py @@ -249,5 +249,36 @@ class Unix(callbacks.Plugin): 'variable appropriately.') wtf = wrap(wtf, [optional(('literal', ['is'])), 'something']) + def ping(self, irc, msg, args, host): + """ + Sends an ICMP echo request to the specified host + """ + pingCmd = self.registryValue('ping.command') + if not pingCmd: + irc.error('The ping command is not configured. If one ' + 'is installed, reconfigure ' + 'supybot.plugins.Unix.ping.command appropriately.', + Raise=True) + else: + try: host = host.group(0) + except AttributeError: pass + + inst = subprocess.Popen([pingCmd,'-c','1', host], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + stdin=file(os.devnull)) + + result = inst.communicate() + + if result[1]: # stderr + irc.reply(' '.join(result[1].split())) + else: + response = result[0].split("\n"); + irc.reply(' '.join(response[1].split()[3:5]).split(':')[0] + + ': ' + ' '.join(response[-3:])) + + _hostExpr = re.compile(r'^[a-z0-9][a-z0-9\.-]*$', re.I) + ping = wrap(ping, [first('ip', ('matches', _hostExpr, 'Invalid hostname'))]) + Class = Unix # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/Unix/test.py b/plugins/Unix/test.py index 062ce550b..b51cd8397 100644 --- a/plugins/Unix/test.py +++ b/plugins/Unix/test.py @@ -64,5 +64,10 @@ if os.name == 'posix': def testFortune(self): self.assertNotError('fortune') + if utils.findBinaryInPath('ping') is not None: + def testPing(self): + self.assertNotError('ping') + + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: