diff --git a/plugins/Unix/config.py b/plugins/Unix/config.py index 69b77ef23..f37cf37e1 100644 --- a/plugins/Unix/config.py +++ b/plugins/Unix/config.py @@ -87,5 +87,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 c4d64c0b9..c69644847 100644 --- a/plugins/Unix/plugin.py +++ b/plugins/Unix/plugin.py @@ -256,5 +256,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 d554d90c7..93b6c0cf8 100644 --- a/plugins/Unix/test.py +++ b/plugins/Unix/test.py @@ -65,5 +65,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: