clicmd_ons.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:taf 作者: taf3 项目源码 文件源码
def _connect_to_switch(self, prompt, timeout=20):
        """SSH connect to switch and wait until prompt string appeared.

        Args:
            prompt(str):  expected CLI prompt.
            timeout(int):  connection timeout.

        Returns:
            None

        Examples::

            self._connect_to_switches(sw_keys=1, prompt="Switch ")

        """
        cli_start_path = ''
        self.suite_logger.debug("Login on switch with login: {0} and expected prompt is: {1}".format(self.login, prompt))
        self.conn.login(self.login, self.passw, timeout=self.timeout)
        self.suite_logger.debug("Create Shell")
        self.conn.open_shell(raw_output=True)
        self.conn.shell.settimeout(self.timeout)
        # lxc: run command "python main.py"
        if self.devtype == 'lxc':
            self.suite_logger.debug("Launched CLI on LXC")
            if os.path.exists(os.path.join(self.build_path, self.img_path, 'main.py')) is True:
                cli_start_path = os.path.join(self.build_path, self.img_path)
                self.conn.shell_command('cd %s && python main.py -a %s -p %s'
                                        % (cli_start_path, self.ipaddr, self.xmlrpcport), timeout=5, ret_code=False, quiet=True)
            else:
                self.suite_logger.error("Path to CLI image does not exist: %s" % (os.path.join(cli_start_path, 'main.py')))
                pytest.exit("Path to CLI image does not exist: %s" % (os.path.join(cli_start_path, 'main.py')))
        else:
            self.suite_logger.debug("Waiting for CLI on Real switch")
            alter = []
            # Add one or few expected prompt(s) and action(s) to alternatives list
            if isinstance(prompt, str):
                prompt = [prompt]
            for single_prompt in prompt:
                alter.append((single_prompt, None, True, False))

            iterations = 12
            for i in range(iterations):
                time.sleep(10)
                login = self.conn.action_on_connect(self.conn.shell, alternatives=alter, timeout=timeout, is_shell=self.is_shell)
                if any(login.find(str(single_prompt)) != -1 for single_prompt in prompt):
                    break
                else:
                    self.suite_logger.debug("Waiting, current prompt is {0}".format(login))
                if i == iterations:
                    login = self.conn.action_on_expect(self.conn.shell, alternatives=alter, timeout=timeout, is_shell=self.is_shell)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号