From 49daa8669624fb4f5f655531d6d3e0bb651b15ab Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Wed, 18 Mar 2026 07:27:59 +0100 Subject: [PATCH] lib.SSHClient: Log more details around exceptions Add a wrapper around urlparse() and Paramiko's connect() function, in order to log some more info in case an exception is thrown. Signed-off-by: Jan Lindemann --- src/python/jw/pkg/lib/SSHClient.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/python/jw/pkg/lib/SSHClient.py b/src/python/jw/pkg/lib/SSHClient.py index 6da2e7a4..846c2e76 100644 --- a/src/python/jw/pkg/lib/SSHClient.py +++ b/src/python/jw/pkg/lib/SSHClient.py @@ -13,7 +13,11 @@ class SSHClient(ExecContext): def __init__(self, uri: str, *args, **kwargs) -> None: super().__init__(uri=uri, *args, **kwargs) - parsed = urlparse(uri) + try: + parsed = urlparse(uri) + except Exception as e: + log(ERR, f'Failed to parse SSH URI "{uri}"') + raise self.__hostname = parsed.hostname self.__password: parsed.password self.__username = parsed.username @@ -103,7 +107,11 @@ class SSHClientInternal(SSHClient): # export ret = paramiko.SSHClient() ret.set_missing_host_key_policy(paramiko.AutoAddPolicy()) path_to_key=os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') - ret.connect(self.hostname, key_filename=path_to_key, allow_agent=True) + try: + ret.connect(self.hostname, key_filename=path_to_key, allow_agent=True) + except Exception as e: + log(ERR, f'Failed to connect to {self.hostname} with key file {path_to_key} ({str(e)})') + raise s = ret.get_transport().open_session() # set up the agent request handler to handle agent requests from the server paramiko.agent.AgentRequestHandler(s) @@ -120,7 +128,11 @@ class SSHClientInternal(SSHClient): # export return SCPClient(self.__ssh.get_transport()) async def _run_ssh(self, cmd: list[str], cmd_input: str|None) -> Result: - stdin, stdout, stderr = self.__ssh.exec_command(shlex.join(cmd), timeout=self.__timeout) + try: + stdin, stdout, stderr = self.__ssh.exec_command(shlex.join(cmd), timeout=self.__timeout) + except Exception as e: + log(ERR, f'Command failed for {self.uri}: "{shlex.join(cmd)}"') + raise if cmd_input is not None: stdin.write(cmd_input) exit_status = stdout.channel.recv_exit_status()