From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga09.intel.com (mga09.intel.com [134.134.136.24]) by dpdk.org (Postfix) with ESMTP id 48891C31A for ; Fri, 22 May 2015 11:05:00 +0200 (CEST) Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga102.jf.intel.com with ESMTP; 22 May 2015 02:04:47 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,474,1427785200"; d="scan'208";a="496956652" Received: from shvmail01.sh.intel.com ([10.239.29.42]) by FMSMGA003.fm.intel.com with ESMTP; 22 May 2015 02:04:46 -0700 Received: from shecgisg003.sh.intel.com (shecgisg003.sh.intel.com [10.239.29.90]) by shvmail01.sh.intel.com with ESMTP id t4M94ijX003099; Fri, 22 May 2015 17:04:44 +0800 Received: from shecgisg003.sh.intel.com (localhost [127.0.0.1]) by shecgisg003.sh.intel.com (8.13.6/8.13.6/SuSE Linux 0.8) with ESMTP id t4M94gpG014405; Fri, 22 May 2015 17:04:44 +0800 Received: (from yliu84x@localhost) by shecgisg003.sh.intel.com (8.13.6/8.13.6/Submit) id t4M94ghd014401; Fri, 22 May 2015 17:04:42 +0800 From: "Jiajia, Sun" To: dts@dpdk.org Date: Fri, 22 May 2015 17:04:05 +0800 Message-Id: <1432285452-14286-13-git-send-email-sunx.jiajia@intel.com> X-Mailer: git-send-email 1.7.4.1 In-Reply-To: <1432285452-14286-1-git-send-email-sunx.jiajia@intel.com> References: <1432285452-14286-1-git-send-email-sunx.jiajia@intel.com> Subject: [dts] [PATCH v2 12/19] Add some codes to make session to support virtual test X-BeenThere: dts@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: test suite reviews and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 22 May 2015 09:05:02 -0000 From: sjiajiax Signed-off-by: sjiajiax --- framework/ssh_pexpect.py | 62 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/framework/ssh_pexpect.py b/framework/ssh_pexpect.py index 735df44..98c3a60 100644 --- a/framework/ssh_pexpect.py +++ b/framework/ssh_pexpect.py @@ -2,7 +2,8 @@ import time import pexpect import pxssh from debugger import ignore_keyintr, aware_keyintr -from exception import TimeoutException, SSHConnectionException +from exception import TimeoutException, SSHConnectionException, SSHSessionDeadException +from utils import RED, GREEN """ Module handle ssh sessions between tester and DUT. @@ -14,16 +15,28 @@ Aslo support transfer files to tester or DUT. class SSHPexpect(object): def __init__(self, host, username, password): - self.magic_prompt = "[MAGIC PROMPT]" + self.magic_prompt = "MAGIC PROMPT" try: self.session = pxssh.pxssh() self.username = username self.host = host self.password = password - self.session.login(self.host, self.username, - self.password, original_prompt='[$#>]') + if ':' in host: + self.ip = host.split(':')[0] + self.port = int(host.split(':')[1]) + self.session.login(self.ip, self.username, + self.password, original_prompt='[$#>]', + port=self.port, login_timeout=20) + else: + self.session.login(self.host, self.username, + self.password, original_prompt='[$#>]') self.send_expect('stty -echo', '# ', timeout=2) - except Exception: + except Exception, e: + print RED(e) + if getattr(self, 'port', None): + suggestion = "\nSuggession: Check if the fireware on [ %s ] " % \ + self.ip + "is stoped\n" + print GREEN(suggestion) raise SSHConnectionException(host) def init_log(self, logger, name): @@ -45,30 +58,54 @@ class SSHPexpect(object): def send_expect(self, command, expected, timeout=15, verify=False): ret = self.send_expect_base(command, expected, timeout) if verify: - ret_status = self.send_expect_base("echo $?", expected) + ret_status = self.send_expect_base("echo $?", expected, timeout) if not int(ret_status): return ret else: self.logger.error("Command: %s failure!" % command) - return -1 + self.logger.error(ret) + return ret_status else: return ret - def __flush(self): + def get_session_before(self, timeout=15): + """ + Get all output before timeout + """ + ignore_keyintr() self.session.PROMPT = self.magic_prompt - self.session.prompt(0.1) + try: + self.session.prompt(timeout) + except Exception as e: + pass + + aware_keyintr() + before = self.get_output_before() + self.__flush() + return before + + def __flush(self): + """ + Clear all session buffer + """ + self.session.buffer = "" + self.session.before = "" def __prompt(self, command, timeout): if not self.session.prompt(timeout): raise TimeoutException(command, self.get_output_all()) def __sendline(self, command): + if not self.isalive(): + raise SSHSessionDeadException(self.host) if len(command) == 2 and command.startswith('^'): self.session.sendcontrol(command[1]) else: self.session.sendline(command) def get_output_before(self): + if not self.isalive(): + raise SSHSessionDeadException(self.host) self.session.flush() before = self.session.before.rsplit('\r\n', 1) if before[0] == "[PEXPECT]": @@ -103,7 +140,12 @@ class SSHPexpect(object): """ Sends a local file to a remote place. """ - command = 'scp {0} {1}@{2}:{3}'.format(src, self.username, self.host, dst) + if ':' in self.host: + command = 'scp -P {0} -o NoHostAuthenticationForLocalhost=yes {1} {2}@{3}:{4}'.format( + str(self.port), src, self.username, self.ip, dst) + else: + command = 'scp {0} {1}@{2}:{3}'.format( + src, self.username, self.host, dst) if password == '': self._spawn_scp(command, self.password) else: -- 1.9.3