Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
else:
logging.info("SSH key pair already exist")
session = self.runner.session
# To avoid the host key checking
ssh_options = "%s %s" % ("-o UserKnownHostsFile=/dev/null",
"-o StrictHostKeyChecking=no")
session.sendline("ssh-copy-id %s -i %s root@%s"
% (ssh_options, pub_key, vm_ip))
while True:
matched_strs = [r"[Aa]re you sure", r"[Pp]assword:\s*$",
r"lost connection", r"]#"]
try:
index, text = session.read_until_last_line_matches(
matched_strs, timeout=timeout,
internal_timeout=0.5)
except (aexpect.ExpectTimeoutError,
aexpect.ExpectProcessTerminatedError) as e:
raise exceptions.TestFail(
"Setup autologin to vm failed:%s" % e)
logging.debug("%s:%s", index, text)
if index == 0:
logging.debug("Sending 'yes' for fingerprint...")
session.sendline("yes")
continue
elif index == 1:
logging.debug("Sending '%s' for vm logging...", vm_pwd)
session.sendline(vm_pwd)
continue
elif index == 2:
raise exceptions.TestFail("Disconnected to vm on remote host.")
elif index == 3:
logging.debug("Logged in now...")
authentication_done = True
continue
elif password_prompt_count == 1 and scp_type == 2:
logging.debug("Got password prompt, sending '%s'" %
password_list[password_prompt_count])
session.sendline(password_list[password_prompt_count])
password_prompt_count += 1
timeout = transfer_timeout
authentication_done = True
continue
else:
raise SCPAuthenticationError("Got password prompt twice",
text)
elif match == 2: # "lost connection"
raise SCPError("SCP client said 'lost connection'", text)
except aexpect.ExpectTimeoutError, e:
if authentication_done:
raise SCPTransferTimeoutError(e.output)
else:
raise SCPAuthenticationTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError, e:
if e.status == 0:
logging.debug("SCP process terminated with status 0")
break
else:
raise SCPTransferFailedError(e.status, e.output)
authentication_done = True
continue
elif password_prompt_count == 1 and scp_type == 2:
logging.debug("Got password prompt, sending '%s'" %
password_list[password_prompt_count])
session.sendline(password_list[password_prompt_count])
password_prompt_count += 1
timeout = transfer_timeout
authentication_done = True
continue
else:
raise SCPAuthenticationError("Got password prompt twice",
text)
elif match == 2: # "lost connection"
raise SCPError("SCP client said 'lost connection'", text)
except aexpect.ExpectTimeoutError, e:
if authentication_done:
raise SCPTransferTimeoutError(e.output)
else:
raise SCPAuthenticationTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError, e:
if e.status == 0:
logging.debug("SCP process terminated with status 0")
break
else:
raise SCPTransferFailedError(e.status, e.output)
elif match == 4: # "Connection refused"
raise LoginError("Client said 'connection refused'", text)
elif match == 5: # "Please wait"
if debug:
logging.debug("Got 'Please wait'")
timeout = 30
continue
elif match == 6: # "Warning added RSA"
if debug:
logging.debug("Got 'Warning added RSA to known host list")
continue
elif match == 7: # prompt
if debug:
logging.debug("Got shell prompt -- logged in")
break
except aexpect.ExpectTimeoutError, e:
raise LoginTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError, e:
raise LoginProcessTerminatedError(e.status, e.output)
terminates while waiting for output
:raise ExpectError: Raised if an unknown error occurs
"""
if not match_func:
match_func = self.match_patterns
fd = self._get_fd("expect")
o = ""
end_time = time.time() + timeout
while True:
try:
r, w, x = select.select([fd], [], [],
max(0, end_time - time.time()))
except (select.error, TypeError):
break
if not r:
raise aexpect.ExpectTimeoutError(patterns, o)
# Read data from child
data = self.read_nonblocking(internal_timeout,
end_time - time.time())
if not data:
break
# Print it if necessary
if print_func:
for line in data.splitlines():
print_func(line)
# Look for patterns
o += data
out = ''
match = match_func(filter_func(o), patterns)
if match is not None:
output = o.splitlines()
authentication_done = True
continue
elif password_prompt_count == 1 and scp_type == 2:
logging.debug("Got password prompt, sending '%s'" %
password_list[password_prompt_count])
session.sendline(password_list[password_prompt_count])
password_prompt_count += 1
timeout = transfer_timeout
authentication_done = True
continue
else:
raise SCPAuthenticationError("Got password prompt twice",
text)
elif match == 2: # "lost connection"
raise SCPError("SCP client said 'lost connection'", text)
except aexpect.ExpectTimeoutError, e:
if authentication_done:
raise SCPTransferTimeoutError(e.output)
else:
raise SCPAuthenticationTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError, e:
if e.status == 0:
logging.debug("SCP process terminated with status 0")
break
else:
raise SCPTransferFailedError(e.status, e.output)
if debug:
logging.debug("Got shell prompt -- logged in")
break
elif match == 13: # console prompt
logging.debug("Got console prompt, send return to show login")
session.sendline()
elif match == 14: # VMware vCenter command prompt
# Some old vsphere version (e.x. 6.0.0) needs to enable first.
cmd = 'shell.set --enabled True'
logging.debug(
"Got VMware VCenter prompt, send '%s' to enable shell first" % cmd)
session.sendline(cmd)
logging.debug(
"Got VMware VCenter prompt, send 'shell' to launch bash")
session.sendline('shell')
except aexpect.ExpectTimeoutError as e:
# sometimes, linux kernel print some message to console
# the message maybe impact match login pattern, so send
# a empty line to avoid unexpect login timeout
if not last_chance:
time.sleep(0.5)
session.sendline()
last_chance = True
continue
else:
raise LoginTimeoutError(e.output)
except aexpect.ExpectProcessTerminatedError as e:
raise LoginProcessTerminatedError(e.status, e.output)
return output
output = process.get_output()
process.close()
return status, output
else:
output = ""
try:
output = session.cmd_output(command, timeout=timeout,
print_func=output_func)
except aexpect.ShellTimeoutError:
# Send ctrl+c (SIGINT) through ssh session
session.send("\003")
try:
output2 = session.read_up_to_prompt(print_func=output_func)
output += output2
except aexpect.ExpectTimeoutError, e:
output += e.output
# We also need to use this session to query the return value
session.send("\003")
session.sendline(session.status_test_command)
try:
o2 = session.read_up_to_prompt()
except aexpect.ExpectError:
status = -1
else:
try:
status = int(re.findall("\d+", o2)[0])
except Exception:
status = -1
return status, output
terminates while waiting for output
:raise ExpectError: Raised if an unknown error occurs
"""
if not match_func:
match_func = self.match_patterns
fd = self._get_fd("expect")
o = ""
end_time = time.time() + timeout
while True:
try:
r, w, x = select.select([fd], [], [],
max(0, end_time - time.time()))
except (select.error, TypeError):
break
if not r:
raise aexpect.ExpectTimeoutError(patterns, o)
# Read data from child
data = self.read_nonblocking(internal_timeout,
end_time - time.time())
if not data:
break
# Print it if necessary
if print_func:
for line in data.splitlines():
print_func(line)
# Look for patterns
o += data
out = ''
match = match_func(filter_func(o), patterns)
if match is not None:
output = o.splitlines()