mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-12-25 04:02:46 +01:00
Unix: various enhancements to the ping command:
Fixed bug in 100% packet loss response. Errors from ping are now sent to irc.error(). Added packet count, interval, ttl, and wait options. Added additional test cases. Also, Enabled threading for Unix plugin, and for wtf, spell, and ping commands. Signed-off-by: Daniel Folkinshteyn <nanotube@users.sourceforge.net>
This commit is contained in:
parent
3a84faeb18
commit
edc4d8644e
@ -66,6 +66,7 @@ def pipeReadline(fd, timeout=2):
|
|||||||
raise TimeoutError
|
raise TimeoutError
|
||||||
|
|
||||||
class Unix(callbacks.Plugin):
|
class Unix(callbacks.Plugin):
|
||||||
|
threaded = True
|
||||||
def errno(self, irc, msg, args, s):
|
def errno(self, irc, msg, args, s):
|
||||||
"""<error number or code>
|
"""<error number or code>
|
||||||
|
|
||||||
@ -181,7 +182,7 @@ class Unix(callbacks.Plugin):
|
|||||||
else:
|
else:
|
||||||
resp = 'Something unexpected was seen in the [ai]spell output.'
|
resp = 'Something unexpected was seen in the [ai]spell output.'
|
||||||
irc.reply(resp)
|
irc.reply(resp)
|
||||||
spell = wrap(spell, ['something'])
|
spell = thread(wrap(spell, ['something']))
|
||||||
|
|
||||||
def fortune(self, irc, msg, args):
|
def fortune(self, irc, msg, args):
|
||||||
"""takes no arguments
|
"""takes no arguments
|
||||||
@ -247,11 +248,14 @@ class Unix(callbacks.Plugin):
|
|||||||
'on this system, reconfigure the '
|
'on this system, reconfigure the '
|
||||||
'supybot.plugins.Unix.wtf.command configuration '
|
'supybot.plugins.Unix.wtf.command configuration '
|
||||||
'variable appropriately.')
|
'variable appropriately.')
|
||||||
wtf = wrap(wtf, [optional(('literal', ['is'])), 'something'])
|
wtf = thread(wrap(wtf, [optional(('literal', ['is'])), 'something']))
|
||||||
|
|
||||||
def ping(self, irc, msg, args, host):
|
def ping(self, irc, msg, args, optlist, host):
|
||||||
"""<host or ip>
|
"""[--c <count>] [--i <interval>] [--t <ttl>] [--W <timeout>] <host or ip>
|
||||||
Sends an ICMP echo request to the specified host
|
Sends an ICMP echo request to the specified host.
|
||||||
|
The arguments correspond with those listed in ping(8). --c is
|
||||||
|
limited to 10 packets or less (default is 5). --i is limited to 5
|
||||||
|
or less. --W is limited to 10 or less.
|
||||||
"""
|
"""
|
||||||
pingCmd = self.registryValue('ping.command')
|
pingCmd = self.registryValue('ping.command')
|
||||||
if not pingCmd:
|
if not pingCmd:
|
||||||
@ -263,26 +267,40 @@ class Unix(callbacks.Plugin):
|
|||||||
try: host = host.group(0)
|
try: host = host.group(0)
|
||||||
except AttributeError: pass
|
except AttributeError: pass
|
||||||
|
|
||||||
|
args = [pingCmd]
|
||||||
|
for opt, val in optlist:
|
||||||
|
if opt == 'c' and val > 10: val = 10
|
||||||
|
if opt == 'i' and val > 5: val = 5
|
||||||
|
if opt == 'W' and val > 10: val = 10
|
||||||
|
args.append('-%s' % opt)
|
||||||
|
args.append(str(val))
|
||||||
|
if '-c' not in args:
|
||||||
|
args.append('-c')
|
||||||
|
args.append('5')
|
||||||
|
args.append(host)
|
||||||
try:
|
try:
|
||||||
inst = subprocess.Popen([pingCmd,'-c','1', host],
|
inst = subprocess.Popen(args, stdout=subprocess.PIPE,
|
||||||
stdout=subprocess.PIPE,
|
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
stdin=file(os.devnull))
|
stdin=file(os.devnull))
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
irc.error('It seems the configured ping command was '
|
irc.error('It seems the configured ping command was '
|
||||||
'not available (%s).' % e, Raise=True)
|
'not available (%s).' % e, Raise=True)
|
||||||
|
|
||||||
result = inst.communicate()
|
result = inst.communicate()
|
||||||
|
|
||||||
if result[1]: # stderr
|
if result[1]: # stderr
|
||||||
irc.reply(' '.join(result[1].split()))
|
irc.error(' '.join(result[1].split()))
|
||||||
else:
|
else:
|
||||||
response = result[0].split("\n");
|
response = result[0].split("\n");
|
||||||
|
if response[1]:
|
||||||
irc.reply(' '.join(response[1].split()[3:5]).split(':')[0]
|
irc.reply(' '.join(response[1].split()[3:5]).split(':')[0]
|
||||||
+ ': ' + ' '.join(response[-3:]))
|
+ ': ' + ' '.join(response[-3:]))
|
||||||
|
else:
|
||||||
|
irc.reply(' '.join(response[0].split()[1:3])
|
||||||
|
+ ': ' + ' '.join(response[-3:]))
|
||||||
|
|
||||||
_hostExpr = re.compile(r'^[a-z0-9][a-z0-9\.-]*$', re.I)
|
_hostExpr = re.compile(r'^[a-z0-9][a-z0-9\.-]*[a-z0-9]$', re.I)
|
||||||
ping = wrap(ping, [first('ip', ('matches', _hostExpr, 'Invalid hostname'))])
|
ping = thread(wrap(ping, [getopts({'c':'positiveInt','i':'float',
|
||||||
|
't':'positiveInt','W':'positiveInt'}),
|
||||||
|
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:
|
||||||
|
@ -66,9 +66,25 @@ if os.name == 'posix':
|
|||||||
|
|
||||||
if utils.findBinaryInPath('ping') is not None:
|
if utils.findBinaryInPath('ping') is not None:
|
||||||
def testPing(self):
|
def testPing(self):
|
||||||
self.assertNotError('unix ping localhost')
|
self.assertNotError('unix ping 127.0.0.1')
|
||||||
self.assertError('unix ping')
|
self.assertError('unix ping')
|
||||||
|
self.assertError('unix ping -localhost')
|
||||||
|
self.assertError('unix ping local%host')
|
||||||
|
def testPingCount(self):
|
||||||
|
self.assertNotError('unix ping --c 1 127.0.0.1')
|
||||||
|
self.assertError('unix ping --c a 127.0.0.1')
|
||||||
|
self.assertRegexp('unix ping --c 11 127.0.0.1','10 packets')
|
||||||
|
self.assertRegexp('unix ping 127.0.0.1','5 packets')
|
||||||
|
def testPingInterval(self):
|
||||||
|
self.assertNotError('unix ping --i 1 --c 1 127.0.0.1')
|
||||||
|
self.assertError('unix ping --i a --c 1 127.0.0.1')
|
||||||
|
# Super-user privileged interval setting
|
||||||
|
self.assertError('unix ping --i 0.1 --c 1 127.0.0.1')
|
||||||
|
def testPingTtl(self):
|
||||||
|
self.assertNotError('unix ping --t 64 --c 1 127.0.0.1')
|
||||||
|
self.assertError('unix ping --t a --c 1 127.0.0.1')
|
||||||
|
def testPingWait(self):
|
||||||
|
self.assertNotError('unix ping --W 1 --c 1 127.0.0.1')
|
||||||
|
self.assertError('unix ping --W a --c 1 127.0.0.1')
|
||||||
|
|
||||||
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|
||||||
|
Loading…
Reference in New Issue
Block a user