Browse code

Split on newlines when searching for become prompt

The fix for leading junk in sudo output: fee6e29 causes problems with
ssh + sudo. On the initial connection using ControlPersist, the output
that we scan for the prompt contains both the command we're sending to
configure the prompt and the prompt itself. The code in fee6e29 ends up
sending the password when it sees the line configuring the prompt which
is too early.

Switch to a version that splits on lines and then checks whether the
first or last line starts with the prompt to decide if it's time to send
the password.

Fixes #23054
References #20858

(cherry picked from commit 6f77498700a989c6b42f4f4d050d98df1de74f9e)

Toshio Kuratomi authored on 2017/03/30 01:34:33
Showing 1 changed files
... ...
@@ -252,10 +252,11 @@ class ConnectionBase(with_metaclass(ABCMeta, object)):
252 252
         if self._play_context.prompt is None:
253 253
             return False
254 254
         elif isinstance(self._play_context.prompt, string_types):
255
-            b_prompt = to_bytes(self._play_context.prompt)
256
-            return b_prompt in b_output
257
-        else:
258
-            return self._play_context.prompt(b_output)
255
+            b_prompt = to_bytes(self._play_context.prompt).strip()
256
+            b_lines = b_output.splitlines(True)
257
+            if not b_lines:
258
+                return False
259
+            return b_lines[-1].strip().endswith(b_prompt) or b_lines[0].strip().endswith(b_prompt)
259 260
 
260 261
     def check_incorrect_password(self, b_output):
261 262
         b_incorrect_password = to_bytes(gettext.dgettext(self._play_context.become_method, C.BECOME_ERROR_STRINGS[self._play_context.become_method]))