Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
routersploit
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
czos-dpend
routersploit
Commits
ac339a95
Commit
ac339a95
authored
Jul 03, 2016
by
fwkz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactoring 'show' command.
parent
a2e5282a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
50 deletions
+59
-50
interpreter.py
routersploit/interpreter.py
+44
-37
test_interpreter.py
routersploit/test/test_interpreter.py
+15
-13
No files found.
routersploit/interpreter.py
View file @
ac339a95
...
...
@@ -352,46 +352,53 @@ class RoutersploitInterpreter(BaseInterpreter):
else
:
yield
opt_key
,
opt_value
,
opt_description
@utils.module_required
def
command_show
(
self
,
*
args
,
**
kwargs
):
info
,
options
,
devices
=
'info'
,
'options'
,
'devices'
sub_command
=
args
[
0
]
if
sub_command
==
info
:
utils
.
pprint_dict_in_order
(
def
_show_info
(
self
,
*
args
,
**
kwargs
):
utils
.
pprint_dict_in_order
(
self
.
module_metadata
,
(
"name"
,
"description"
,
"devices"
,
"authors"
,
"references"
),
)
utils
.
print_info
()
elif
sub_command
==
options
:
target_opts
=
{
'port'
,
'target'
}
module_opts
=
set
(
self
.
current_module
.
options
)
-
target_opts
headers
=
(
"Name"
,
"Current settings"
,
"Description"
)
utils
.
print_info
(
'
\n
Target options:'
)
utils
.
print_table
(
headers
,
*
self
.
get_opts
(
*
target_opts
))
if
module_opts
:
utils
.
print_info
(
'
\n
Module options:'
)
utils
.
print_table
(
headers
,
*
self
.
get_opts
(
*
module_opts
))
utils
.
print_info
()
elif
sub_command
==
devices
:
if
devices
in
self
.
current_module
.
_Exploit__info__
.
keys
():
devices
=
self
.
current_module
.
_Exploit__info__
[
'devices'
]
print
(
"
\n
Target devices:"
)
i
=
0
for
device
in
devices
:
if
isinstance
(
device
,
dict
):
print
(
" {} - {}"
.
format
(
i
,
device
[
'name'
]))
else
:
print
(
" {} - {}"
.
format
(
i
,
device
))
i
+=
1
print
()
else
:
print
(
"
\n
Target devices are not defined"
)
else
:
print
(
"Unknown command 'show {}'. You want to 'show {}' or 'show {}'?"
.
format
(
sub_command
,
info
,
options
))
utils
.
print_info
()
def
_show_options
(
self
,
*
args
,
**
kwargs
):
target_opts
=
{
'port'
,
'target'
}
module_opts
=
set
(
self
.
current_module
.
options
)
-
target_opts
headers
=
(
"Name"
,
"Current settings"
,
"Description"
)
utils
.
print_info
(
'
\n
Target options:'
)
utils
.
print_table
(
headers
,
*
self
.
get_opts
(
*
target_opts
))
if
module_opts
:
utils
.
print_info
(
'
\n
Module options:'
)
utils
.
print_table
(
headers
,
*
self
.
get_opts
(
*
module_opts
))
utils
.
print_info
()
def
_show_devices
(
self
,
*
args
,
**
kwargs
):
# TODO: cover with tests
try
:
devices
=
self
.
current_module
.
_Exploit__info__
[
'devices'
]
print
(
"
\n
Target devices:"
)
i
=
0
for
device
in
devices
:
if
isinstance
(
device
,
dict
):
print
(
" {} - {}"
.
format
(
i
,
device
[
'name'
]))
else
:
print
(
" {} - {}"
.
format
(
i
,
device
))
i
+=
1
print
()
except
KeyError
:
print
(
"
\n
Target devices are not defined"
)
@utils.module_required
def
command_show
(
self
,
*
args
,
**
kwargs
):
sub_commands
=
(
'info'
,
'options'
,
'devices'
)
sub_command
=
args
[
0
]
try
:
getattr
(
self
,
"_show_{}"
.
format
(
sub_command
))(
*
args
,
**
kwargs
)
except
AttributeError
:
utils
.
print_error
(
"Unknown 'show' sub-command '{}'. "
"What do you want to show?
\n
"
"Possible choices are: {}"
.
format
(
sub_command
,
sub_commands
))
@utils.stop_after
(
2
)
def
complete_show
(
self
,
text
,
*
args
,
**
kwargs
):
...
...
routersploit/test/test_interpreter.py
View file @
ac339a95
...
...
@@ -356,7 +356,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self
.
assertEqual
(
self
.
interpreter
.
current_module
,
None
)
@mock.patch
(
'__builtin__.print'
)
def
test_
command_
show_info
(
self
,
mock_print
):
def
test_show_info
(
self
,
mock_print
):
metadata
=
{
'devices'
:
'target_desc'
,
'authors'
:
'authors_desc'
,
...
...
@@ -368,7 +368,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self
.
interpreter
.
current_module
.
__doc__
=
description
self
.
interpreter
.
current_module
.
_MagicMock__info__
=
metadata
self
.
interpreter
.
command_show
(
'info'
)
self
.
interpreter
.
_show_info
(
)
self
.
assertEqual
(
mock_print
.
mock_calls
,
[
...
...
@@ -392,7 +392,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self
.
interpreter
.
current_module
.
__doc__
=
description
self
.
interpreter
.
current_module
.
_MagicMock__info__
=
metadata
self
.
interpreter
.
command_show
(
'info'
)
self
.
interpreter
.
_show_info
(
)
self
.
assertEqual
(
mock_print
.
mock_calls
,
[
...
...
@@ -400,7 +400,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
)
@mock.patch
(
'__builtin__.print'
)
def
test_
command_
show_options
(
self
,
mock_print
):
def
test_show_options
(
self
,
mock_print
):
exploit_attributes
=
{
'target'
:
'target_desc'
,
'port'
:
'port_desc'
,
...
...
@@ -417,7 +417,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self
.
interpreter
.
current_module
.
target
=
'127.0.0.1'
self
.
interpreter
.
current_module
.
port
=
22
self
.
interpreter
.
command_show
(
'options'
)
self
.
interpreter
.
_show_options
(
)
self
.
assertEqual
(
mock_print
.
mock_calls
,
[
...
...
@@ -452,7 +452,7 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
self
.
interpreter
.
current_module
.
target
=
'127.0.0.1'
self
.
interpreter
.
current_module
.
port
=
22
self
.
interpreter
.
command_show
(
'options'
)
self
.
interpreter
.
_show_options
(
)
self
.
assertEqual
(
mock_print
.
mock_calls
,
[
...
...
@@ -467,15 +467,17 @@ class RoutersploitInterpreterTest(RoutersploitTestCase):
]
)
@mock.patch
(
'__builtin__.print'
)
def
test_command_show_unknown_sub_command
(
self
,
mock_print
):
help_text
=
"Unknown command 'show unknown_sub_command'. You want to 'show info' or 'show options'?"
def
test_command_show
(
self
):
with
mock
.
patch
.
object
(
self
.
interpreter
,
"_show_options"
)
as
mock_show_options
:
self
.
interpreter
.
command_show
(
"options"
)
mock_show_options
.
assert_called_once_with
(
"options"
)
@mock.patch
(
'routersploit.utils.print_error'
)
def
test_command_show_unknown_sub_command
(
self
,
mock_print_error
):
self
.
interpreter
.
command_show
(
'unknown_sub_command'
)
self
.
assertEqual
(
mock_print
.
mock_calls
,
[
mock
.
call
(
help_text
)]
)
mock_print_error
.
assert_called_once_with
(
"Unknown 'show' sub-command 'unknown_sub_command'. "
"What do you want to show?
\n
"
"Possible choices are: ('info', 'options', 'devices')"
)
def
test_if_command_run_has_module_required_decorator
(
self
):
self
.
assertIsDecorated
(
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment