Added ping command to Unix plugin

Signed-off-by: Daniel Folkinshteyn <nanotube@users.sourceforge.net>
This commit is contained in:
oevna@users.sourceforge.net 2010-07-08 17:17:37 -04:00 committed by Valentin Lorentz
parent f6c9543dc3
commit 390b3ec15f
3 changed files with 40 additions and 0 deletions

View File

@ -87,5 +87,9 @@ conf.registerGlobalValue(Unix.wtf, 'command',
registry.String(utils.findBinaryInPath('wtf') or '', _("""Determines what registry.String(utils.findBinaryInPath('wtf') or '', _("""Determines what
command will be called for the wtf command."""))) 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: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -256,5 +256,36 @@ class Unix(callbacks.Plugin):
'variable appropriately.')) 'variable appropriately.'))
wtf = wrap(wtf, [optional(('literal', ['is'])), 'something']) wtf = wrap(wtf, [optional(('literal', ['is'])), 'something'])
def ping(self, irc, msg, args, host):
"""<host or ip>
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 Class = Unix
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -65,5 +65,10 @@ if os.name == 'posix':
def testFortune(self): def testFortune(self):
self.assertNotError('fortune') 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: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: