Commit 7e8a7a99 by fwkz

Sorting commands in command suggestions.

parent 6b40a74d
......@@ -121,4 +121,3 @@ class Exploit(object):
def __str__(self):
return self.__module__.split('.', 2).pop().replace('.', os.sep)
......@@ -175,6 +175,11 @@ class RoutersploitInterpreter(BaseInterpreter):
self.prompt_hostname = 'rsf'
self.show_sub_commands = ('info', 'options', 'devices', 'all', 'creds', 'exploits', 'scanners')
self.global_commands = sorted(['use ', 'exec ', 'help', 'exit', 'show '])
self.module_commands = ['run', 'back', 'set ', 'setg ', 'check']
self.module_commands.extend(self.global_commands)
self.module_commands.sort()
self.modules = utils.index_modules()
self.main_modules_dirs = [module for module in os.listdir(utils.MODULES_DIR) if not module.startswith("__")]
......@@ -243,20 +248,19 @@ class RoutersploitInterpreter(BaseInterpreter):
matches.add("".join((text, head, sep)))
return list(map(utils.humanize_path, matches)) # humanize output, replace dots to forward slashes
def suggested_commands(self): # TODO: sorted list, factor out generic commands
def suggested_commands(self):
""" Entry point for intelligent tab completion.
Based on state of interpreter this method will return intelligent suggestions.
:return: list of most accurate command suggestions
"""
if self.current_module:
module_commands = ['run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit']
if GLOBAL_OPTS.keys():
return itertools.chain(module_commands, ('unsetg ',))
return module_commands
if self.current_module and GLOBAL_OPTS:
return sorted(itertools.chain(self.module_commands, ('unsetg ',)))
elif self.current_module:
return self.module_commands
else:
return ['use ', 'exec', 'help', 'exit', 'show ']
return self.global_commands
def command_back(self, *args, **kwargs):
self.current_module = None
......
......@@ -89,7 +89,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.set_module()
self.rsf.send("\t\t")
self.assertPrompt(
'back check exec exit help run set setg show \r\n',
' exec exit help run set setg show use \r\n',
self.module_prompt('FTP Bruteforce')
)
......@@ -182,7 +182,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.set_module()
self.rsf.send("\t\t")
self.assertPrompt(
" check exec exit help run set setg show \r\n",
" exec exit help run set setg show use \r\n",
self.module_prompt('FTP Bruteforce'),
)
......@@ -194,7 +194,7 @@ class RoutersploitCompleterTest(RoutersploitTestCase):
self.rsf.send("setg target foo\r\n")
self.rsf.send("\t\t")
self.assertPrompt(
" show \r\ncheck exit run setg unsetg \r\n",
' use \r\ncheck exit run setg unsetg \r\n',
self.module_prompt('FTP Bruteforce'),
)
......
......@@ -254,23 +254,23 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self.assertEqual(self.module_prompt_default('UnnamedModule'), self.interpreter.prompt)
def test_suggested_commands_with_loaded_module_and_no_global_value_set(self):
self.assertEqual(
self.assertItemsEqual(
list(self.interpreter.suggested_commands()),
['run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit'] # Extra space at the end because of following param
['use ', 'run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit'] # Extra space at the end because of following param
)
def test_suggested_commands_with_loaded_module_and_global_value_set(self):
GLOBAL_OPTS['key'] = 'value'
self.assertEqual(
self.assertItemsEqual(
list(self.interpreter.suggested_commands()),
['run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit', 'unsetg '] # Extra space at the end because of following param
['use ', 'run', 'back', 'set ', 'setg ', 'show ', 'check', 'exec ', 'help', 'exit', 'unsetg '] # Extra space at the end because of following param
)
def test_suggested_commands_without_loaded_module(self):
self.interpreter.current_module = None
self.assertEqual(
self.interpreter.suggested_commands(), # Extra space at the end because of following param
['use ', 'exec', 'help', 'exit', 'show ']
['exec ', 'exit', 'help', 'show ', 'use ']
)
@mock.patch('importlib.import_module')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment