Skip to content

Commit 169ea31

Browse files
Land rapid7#18930, Adds a help command within the interactive query prompt
2 parents 0139ed6 + 2a68e04 commit 169ea31

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/rex/post/sql/ui/console/command_dispatcher/client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ def cmd_query_interactive_help
6767
print_line
6868
end
6969

70+
def query_interactive_help
71+
print_line 'Interactive SQL prompt'
72+
print_line
73+
print_line 'You are in an interactive SQL shell where SQL queries can be executed.'
74+
print_line 'SQL commands ending with ; will be executed on the remote server.'
75+
print_line "To exit, type 'exit', 'quit', 'end' or 'stop'."
76+
print_line
77+
end
78+
7079
def cmd_query_interactive(*args)
7180
if help_args?(args)
7281
cmd_query_interactive_help

lib/rex/post/sql/ui/console/interactive_sql_client.rb

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def _interact
2222
while self.interacting
2323
sql_input = _multiline_with_fallback
2424
self.interacting = (sql_input[:status] != :exit)
25+
26+
if sql_input[:status] == :help
27+
client_dispatcher.query_interactive_help
28+
end
29+
2530
# We need to check that the user is still interacting, i.e. if ctrl+z is triggered when requesting user input
2631
break unless (self.interacting && sql_input[:result])
2732

@@ -77,8 +82,10 @@ def _multiline
7782
end
7883

7984
stop_words = %w[stop s exit e end quit q].freeze
85+
help_words = %w[help h].freeze
8086

8187
finished = false
88+
help = false
8289
begin
8390
result = nil
8491
prompt_proc_before = ::Reline.prompt_proc
@@ -100,10 +107,14 @@ def _multiline
100107
next true
101108
end
102109

103-
# In the case only a stop word was input, exit out of the REPL shell
104-
finished = (multiline_input.split.count == 1 && stop_words.include?(multiline_input.split.last))
110+
if multiline_input.split.count == 1
111+
# In the case only a stop word was input, exit out of the REPL shell
112+
finished = stop_words.include?(multiline_input.split.last)
113+
# In the case when only a help word was input call the help command
114+
help = help_words.include?(multiline_input.split.last)
115+
end
105116

106-
finished || multiline_input.split.last&.end_with?(';')
117+
finished || help || multiline_input.split.last&.end_with?(';')
107118
end
108119
rescue ::StandardError => e
109120
elog('Failed to get multi-line SQL query from user', e)
@@ -115,6 +126,10 @@ def _multiline
115126
return result
116127
end
117128

129+
if help
130+
return { status: :help, result: nil }
131+
end
132+
118133
if finished
119134
self.interacting = false
120135
print_status 'Exiting Interactive mode.'

0 commit comments

Comments
 (0)