From patchwork Mon Jan 3 18:27:03 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 448551 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p03IQlhQ023945 for ; Mon, 3 Jan 2011 18:26:49 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751324Ab1ACS0q (ORCPT ); Mon, 3 Jan 2011 13:26:46 -0500 Received: from mx1.redhat.com ([209.132.183.28]:3486 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750766Ab1ACS0p (ORCPT ); Mon, 3 Jan 2011 13:26:45 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id p03IQiHq024080 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 3 Jan 2011 13:26:44 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id p03IQhqw021774; Mon, 3 Jan 2011 13:26:43 -0500 Received: from moof.tlv.redhat.com (dhcp-1-185.tlv.redhat.com [10.35.1.185]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id p03IQeWU027298; Mon, 3 Jan 2011 13:26:42 -0500 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH 02/17] KVM test: kvm_utils.py: reorder remote_login(), remote_scp(), copy_files_to(), etc Date: Mon, 3 Jan 2011 20:27:03 +0200 Message-Id: <1294079238-21239-2-git-send-email-mgoldish@redhat.com> In-Reply-To: <1294079238-21239-1-git-send-email-mgoldish@redhat.com> References: <1294079238-21239-1-git-send-email-mgoldish@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter1.kernel.org [140.211.167.41]); Mon, 03 Jan 2011 18:26:50 +0000 (UTC) diff --git a/client/tests/kvm/kvm_utils.py b/client/tests/kvm/kvm_utils.py index 41117e3..c2918c9 100644 --- a/client/tests/kvm/kvm_utils.py +++ b/client/tests/kvm/kvm_utils.py @@ -575,6 +575,49 @@ def _remote_login(session, username, password, prompt, timeout=10): return False +def remote_login(client, host, port, username, password, prompt, linesep="\n", + log_filename=None, timeout=10): + """ + Log into a remote host (guest) using SSH/Telnet/Netcat. + + @param client: The client to use ('ssh', 'telnet' or 'nc') + @param host: Hostname or IP address + @param port: Port to connect to + @param username: Username (if required) + @param password: Password (if required) + @param prompt: Shell prompt (regular expression) + @param linesep: The line separator to use when sending lines + (e.g. '\\n' or '\\r\\n') + @param log_filename: If specified, log all output to this file + @param timeout: The maximal time duration (in seconds) to wait for + each step of the login procedure (i.e. the "Are you sure" prompt + or the password prompt) + + @return: ShellSession object on success and None on failure. + """ + if client == "ssh": + cmd = ("ssh -o UserKnownHostsFile=/dev/null " + "-o PreferredAuthentications=password -p %s %s@%s" % + (port, username, host)) + elif client == "telnet": + cmd = "telnet -l %s %s %s" % (username, host, port) + elif client == "nc": + cmd = "nc %s %s" % (host, port) + else: + logging.error("Unknown remote shell client: %s" % client) + return + + logging.debug("Trying to login with command '%s'" % cmd) + session = kvm_subprocess.ShellSession(cmd, linesep=linesep, prompt=prompt) + if _remote_login(session, username, password, prompt, timeout): + if log_filename: + session.set_output_func(log_line) + session.set_output_params((log_filename,)) + return session + else: + session.close() + + def _remote_scp(session, password, transfer_timeout=600, login_timeout=10): """ Transfer file(s) to a remote host (guest) using SCP. Wait for questions @@ -627,49 +670,6 @@ def _remote_scp(session, password, transfer_timeout=600, login_timeout=10): return e.status == 0 -def remote_login(client, host, port, username, password, prompt, linesep="\n", - log_filename=None, timeout=10): - """ - Log into a remote host (guest) using SSH/Telnet/Netcat. - - @param client: The client to use ('ssh', 'telnet' or 'nc') - @param host: Hostname or IP address - @param port: Port to connect to - @param username: Username (if required) - @param password: Password (if required) - @param prompt: Shell prompt (regular expression) - @param linesep: The line separator to use when sending lines - (e.g. '\\n' or '\\r\\n') - @param log_filename: If specified, log all output to this file - @param timeout: The maximal time duration (in seconds) to wait for - each step of the login procedure (i.e. the "Are you sure" prompt - or the password prompt) - - @return: ShellSession object on success and None on failure. - """ - if client == "ssh": - cmd = ("ssh -o UserKnownHostsFile=/dev/null " - "-o PreferredAuthentications=password -p %s %s@%s" % - (port, username, host)) - elif client == "telnet": - cmd = "telnet -l %s %s %s" % (username, host, port) - elif client == "nc": - cmd = "nc %s %s" % (host, port) - else: - logging.error("Unknown remote shell client: %s" % client) - return - - logging.debug("Trying to login with command '%s'" % cmd) - session = kvm_subprocess.ShellSession(cmd, linesep=linesep, prompt=prompt) - if _remote_login(session, username, password, prompt, timeout): - if log_filename: - session.set_output_func(log_line) - session.set_output_params((log_filename,)) - return session - else: - session.close() - - def remote_scp(command, password, log_filename=None, transfer_timeout=600, login_timeout=10): """ @@ -708,10 +708,54 @@ def remote_scp(command, password, log_filename=None, transfer_timeout=600, session.close() +def scp_to_remote(host, port, username, password, local_path, remote_path, + log_filename=None, timeout=600): + """ + Copy files to a remote host (guest) through scp. + + @param host: Hostname or IP address + @param username: Username (if required) + @param password: Password (if required) + @param local_path: Path on the local machine where we are copying from + @param remote_path: Path on the remote machine where we are copying to + @param log_filename: If specified, log all output to this file + @param timeout: The time duration (in seconds) to wait for the transfer + to complete. + + @return: True on success and False on failure. + """ + command = ("scp -v -o UserKnownHostsFile=/dev/null " + "-o PreferredAuthentications=password -r -P %s %s %s@%s:%s" % + (port, local_path, username, host, remote_path)) + return remote_scp(command, password, log_filename, timeout) + + +def scp_from_remote(host, port, username, password, remote_path, local_path, + log_filename=None, timeout=600): + """ + Copy files from a remote host (guest). + + @param host: Hostname or IP address + @param username: Username (if required) + @param password: Password (if required) + @param local_path: Path on the local machine where we are copying from + @param remote_path: Path on the remote machine where we are copying to + @param log_filename: If specified, log all output to this file + @param timeout: The time duration (in seconds) to wait for the transfer + to complete. + + @return: True on success and False on failure. + """ + command = ("scp -v -o UserKnownHostsFile=/dev/null " + "-o PreferredAuthentications=password -r -P %s %s@%s:%s %s" % + (port, username, host, remote_path, local_path)) + return remote_scp(command, password, log_filename, timeout) + + def copy_files_to(address, client, username, password, port, local_path, remote_path, log_filename=None, timeout=600): """ - Decide the transfer cleint and copy file to a remote host (guest). + Copy files to a remote host (guest) using the selected client. @param client: Type of transfer client @param username: Username (if required) @@ -741,9 +785,9 @@ def copy_files_to(address, client, username, password, port, local_path, def copy_files_from(address, client, username, password, port, local_path, - remote_path, log_filename=None, timeout=600): + remote_path, log_filename=None, timeout=600): """ - Decide the transfer cleint and copy file from a remote host (guest). + Copy files from a remote host (guest) using the selected client. @param client: Type of transfer client @param username: Username (if required) @@ -772,50 +816,6 @@ def copy_files_from(address, client, username, password, port, local_path, return True -def scp_to_remote(host, port, username, password, local_path, remote_path, - log_filename=None, timeout=600): - """ - Copy files to a remote host (guest) through scp. - - @param host: Hostname or IP address - @param username: Username (if required) - @param password: Password (if required) - @param local_path: Path on the local machine where we are copying from - @param remote_path: Path on the remote machine where we are copying to - @param log_filename: If specified, log all output to this file - @param timeout: The time duration (in seconds) to wait for the transfer - to complete. - - @return: True on success and False on failure. - """ - command = ("scp -v -o UserKnownHostsFile=/dev/null " - "-o PreferredAuthentications=password -r -P %s %s %s@%s:%s" % - (port, local_path, username, host, remote_path)) - return remote_scp(command, password, log_filename, timeout) - - -def scp_from_remote(host, port, username, password, remote_path, local_path, - log_filename=None, timeout=600): - """ - Copy files from a remote host (guest). - - @param host: Hostname or IP address - @param username: Username (if required) - @param password: Password (if required) - @param local_path: Path on the local machine where we are copying from - @param remote_path: Path on the remote machine where we are copying to - @param log_filename: If specified, log all output to this file - @param timeout: The time duration (in seconds) to wait for the transfer - to complete. - - @return: True on success and False on failure. - """ - command = ("scp -v -o UserKnownHostsFile=/dev/null " - "-o PreferredAuthentications=password -r -P %s %s@%s:%s %s" % - (port, username, host, remote_path, local_path)) - return remote_scp(command, password, log_filename, timeout) - - # The following are utility functions related to ports. def is_port_free(port, address):