Supporting get_pty in ssh_connection.send()

Change-Id: I515a8701cfc4af56a81f089caf256618a4e03612
Signed-off-by: croy <Christian.Roy@windriver.com>
This commit is contained in:
croy
2025-04-15 15:01:34 -04:00
parent 5e87812d9d
commit ba6a7a18c5

View File

@@ -136,7 +136,7 @@ class SSHConnection:
return is_connection_success
def send(self, cmd: str, reconnect_timeout: int = 600) -> str:
def send(self, cmd: str, reconnect_timeout: int = 600, get_pty: bool = False) -> str:
"""
Send a command to the SSH session and return the output.
@@ -146,11 +146,12 @@ class SSHConnection:
Args:
cmd (str): The command to execute.
reconnect_timeout (int): Time in seconds to retry the connection.
get_pty (bool): Defaults to False. Whether to request a terminal when running a 'send' command.
Returns:
str: The output of the command.
"""
return self._execute_command("SEND", cmd, reconnect_timeout=reconnect_timeout)
return self._execute_command("SEND", cmd, reconnect_timeout=reconnect_timeout, get_pty=get_pty)
def send_as_sudo(self, cmd: str, reconnect_timeout: int = 600) -> str:
"""
@@ -187,6 +188,7 @@ class SSHConnection:
cmd: str,
reconnect_timeout: int = 600,
prompts: List[PromptResponse] = None,
get_pty: bool = False,
) -> str:
"""
Executes the given action with the given command.
@@ -198,6 +200,7 @@ class SSHConnection:
cmd (str): The command to run.
reconnect_timeout (int): The time in seconds to wait for SSH connection.
prompts (List[PromptResponse], optional): Expected prompts, if any.
get_pty (bool): Defaults to False. Whether to request a terminal when running a 'send' command.
Returns:
str: The output of the command.
@@ -223,7 +226,7 @@ class SSHConnection:
thread_manager = ThreadManager(timeout=reconnect_timeout / 10)
if action == "SEND":
thread_manager.start_thread("SSH_Command", self._send, cmd)
thread_manager.start_thread("SSH_Command", self._send, cmd, get_pty)
elif action == "SEND_SUDO":
thread_manager.start_thread("SSH_Command", self._send_as_sudo, cmd)
elif action == "SEND_EXPECT_PROMPTS":
@@ -244,20 +247,21 @@ class SSHConnection:
time.sleep(refresh_timeout)
self.is_connected = False
def _send(self, cmd: str, timeout: int = 30) -> str:
def _send(self, cmd: str, timeout: int = 30, get_pty: bool = False) -> str:
"""
Sends the given command with the specified timeout.
Args:
cmd (str): The command to send.
timeout (int): The timeout in seconds for command execution.
get_pty (bool): Defaults to False. Whether to request a terminal when running a 'send' command.
Returns:
str: The output of the command.
"""
get_logger().log_ssh(cmd)
stdin, stdout, stderr = self.client.exec_command(cmd, timeout=timeout)
stdin, stdout, stderr = self.client.exec_command(cmd, timeout=timeout, get_pty=get_pty)
stdout.channel.set_combine_stderr(True)
self.last_return_code = stdout.channel.recv_exit_status()