diff mbox

[KVM-AUTOTEST,05/06] kvm_utils.py: remote_login(): improve regular expression matching

Message ID 1244507691-9575-5-git-send-email-lmr@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Lucas Meneghel Rodrigues June 9, 2009, 12:34 a.m. UTC
1. Make the 'login:' regular expression stricter so it doesn't match
'Last login: ...' messages.
2. Make the 'password:' regular expression stricter.
3. Handle 'Connection refused' messages.

Signed-off-by: Michael Goldish <mgoldish@redhat.com>
---
 client/tests/kvm/kvm_utils.py |   14 +++++++++-----
 1 files changed, 9 insertions(+), 5 deletions(-)

Comments

Lucas Meneghel Rodrigues June 10, 2009, 7:30 p.m. UTC | #1
On Mon, 2009-06-08 at 21:34 -0300, Lucas Meneghel Rodrigues wrote:
> 1. Make the 'login:' regular expression stricter so it doesn't match
> 'Last login: ...' messages.
> 2. Make the 'password:' regular expression stricter.
> 3. Handle 'Connection refused' messages.

Applied, thanks!

> Signed-off-by: Michael Goldish <mgoldish@redhat.com>
> ---
>  client/tests/kvm/kvm_utils.py |   14 +++++++++-----
>  1 files changed, 9 insertions(+), 5 deletions(-)
> 
> diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
> index 434190d..e9d90ac 100644
> --- a/client/tests/kvm/kvm_utils.py
> +++ b/client/tests/kvm/kvm_utils.py
> @@ -539,8 +539,8 @@ def remote_login(command, password, prompt, linesep="\n", timeout=10):
>  
>      while True:
>          (match, text) = sub.read_until_last_line_matches(
> -                ["[Aa]re you sure", "[Pp]assword:", "[Ll]ogin:",
> -                 "[Cc]onnection.*closed", prompt],
> +                [r"[Aa]re you sure", r"[Pp]assword:\s*$", r"^\s*[Ll]ogin:\s*$",
> +                 r"[Cc]onnection.*closed", r"[Cc]onnection.*refused", prompt],
>                   timeout=timeout, internal_timeout=0.5)
>          if match == 0:  # "Are you sure you want to continue connecting"
>              logging.debug("Got 'Are you sure...'; sending 'yes'")
> @@ -564,11 +564,15 @@ def remote_login(command, password, prompt, linesep="\n", timeout=10):
>              logging.debug("Got 'Connection closed'")
>              sub.close()
>              return None
> -        elif match == 4:  # prompt
> +        elif match == 4:  # "Connection refused"
> +            kvm_log.debug("Got 'Connection refused'")
> +            sub.close()
> +            return None
> +        elif match == 5:  # prompt
>              logging.debug("Got shell prompt -- logged in")
>              return sub
>          else:  # match == None
> -            logging.debug("Timeout or process terminated")
> +            logging.debug("Timeout elapsed or process terminated")
>              sub.close()
>              return None
>  
> @@ -602,7 +606,7 @@ def remote_scp(command, password, timeout=300, login_timeout=10):
>  
>      while True:
>          (match, text) = sub.read_until_last_line_matches(
> -                ["[Aa]re you sure", "[Pp]assword:", "lost connection"],
> +                [r"[Aa]re you sure", r"[Pp]assword:\s*$", r"lost connection"],
>                  timeout=_timeout, internal_timeout=0.5)
>          if match == 0:  # "Are you sure you want to continue connecting"
>              logging.debug("Got 'Are you sure...'; sending 'yes'")
diff mbox

Patch

diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py
index 434190d..e9d90ac 100644
--- a/client/tests/kvm/kvm_utils.py
+++ b/client/tests/kvm/kvm_utils.py
@@ -539,8 +539,8 @@  def remote_login(command, password, prompt, linesep="\n", timeout=10):
 
     while True:
         (match, text) = sub.read_until_last_line_matches(
-                ["[Aa]re you sure", "[Pp]assword:", "[Ll]ogin:",
-                 "[Cc]onnection.*closed", prompt],
+                [r"[Aa]re you sure", r"[Pp]assword:\s*$", r"^\s*[Ll]ogin:\s*$",
+                 r"[Cc]onnection.*closed", r"[Cc]onnection.*refused", prompt],
                  timeout=timeout, internal_timeout=0.5)
         if match == 0:  # "Are you sure you want to continue connecting"
             logging.debug("Got 'Are you sure...'; sending 'yes'")
@@ -564,11 +564,15 @@  def remote_login(command, password, prompt, linesep="\n", timeout=10):
             logging.debug("Got 'Connection closed'")
             sub.close()
             return None
-        elif match == 4:  # prompt
+        elif match == 4:  # "Connection refused"
+            kvm_log.debug("Got 'Connection refused'")
+            sub.close()
+            return None
+        elif match == 5:  # prompt
             logging.debug("Got shell prompt -- logged in")
             return sub
         else:  # match == None
-            logging.debug("Timeout or process terminated")
+            logging.debug("Timeout elapsed or process terminated")
             sub.close()
             return None
 
@@ -602,7 +606,7 @@  def remote_scp(command, password, timeout=300, login_timeout=10):
 
     while True:
         (match, text) = sub.read_until_last_line_matches(
-                ["[Aa]re you sure", "[Pp]assword:", "lost connection"],
+                [r"[Aa]re you sure", r"[Pp]assword:\s*$", r"lost connection"],
                 timeout=_timeout, internal_timeout=0.5)
         if match == 0:  # "Are you sure you want to continue connecting"
             logging.debug("Got 'Are you sure...'; sending 'yes'")