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)
评论列表
文章目录