MCPcopy Create free account
hub / github.com/saltstack/salt / _exec_ssh_cmd

Function _exec_ssh_cmd

salt/utils/cloud.py:2088–2139  ·  view source on GitHub ↗
(cmd, error_msg=None, allow_failure=False, **kwargs)

Source from the content-addressed store, hash-verified

2086
2087
2088def _exec_ssh_cmd(cmd, error_msg=None, allow_failure=False, **kwargs):
2089 if error_msg is None:
2090 error_msg = "A wrong password has been issued while establishing ssh session."
2091 password_retries = kwargs.get("password_retries", 3)
2092 try:
2093 stdout, stderr = None, None
2094 proc = salt.utils.vt.Terminal(
2095 cmd,
2096 shell=True,
2097 log_stdout=True,
2098 log_stderr=True,
2099 stream_stdout=kwargs.get("display_ssh_output", True),
2100 stream_stderr=kwargs.get("display_ssh_output", True),
2101 )
2102 sent_password = 0
2103 while proc.has_unread_data:
2104 stdout, stderr = proc.recv()
2105 if stdout and SSH_PASSWORD_PROMP_RE.search(stdout):
2106 # if authenticating with an SSH key and 'sudo' is found
2107 # in the password prompt
2108 if (
2109 "key_filename" in kwargs
2110 and kwargs["key_filename"]
2111 and SSH_PASSWORD_PROMP_SUDO_RE.search(stdout)
2112 ):
2113 proc.sendline(kwargs["sudo_password"])
2114 # elif authenticating via password and haven't exhausted our
2115 # password_retires
2116 elif kwargs.get("password", None) and (
2117 sent_password < password_retries
2118 ):
2119 sent_password += 1
2120 proc.sendline(kwargs["password"])
2121 # else raise an error as we are not authenticating properly
2122 # * not authenticating with an SSH key
2123 # * not authenticating with a Password
2124 else:
2125 raise SaltCloudPasswordError(error_msg)
2126 # 0.0125 is really too fast on some systems
2127 time.sleep(0.5)
2128 if proc.exitstatus != 0 and allow_failure is False:
2129 raise SaltCloudSystemExit(
2130 f"Command '{cmd}' failed. Exit code: {proc.exitstatus}"
2131 )
2132 return proc.exitstatus
2133 except salt.utils.vt.TerminalException as err:
2134 trace = traceback.format_exc()
2135 log.error(error_msg.format(cmd, err, trace))
2136 finally:
2137 proc.close(terminate=True, kill=True)
2138 # Signal an error
2139 return 1
2140
2141
2142def scp_file(dest_path, contents=None, kwargs=None, local_file=None):

Callers 4

wait_for_portFunction · 0.85
scp_fileFunction · 0.85
sftp_fileFunction · 0.85
root_cmdFunction · 0.85

Calls 10

recvMethod · 0.95
sendlineMethod · 0.95
closeMethod · 0.95
SaltCloudSystemExitClass · 0.90
formatMethod · 0.80
getMethod · 0.45
searchMethod · 0.45
sleepMethod · 0.45
errorMethod · 0.45

Tested by

no test coverage detected