2424import datetime
2525import getpass
2626import logging
27- import os
2827import shlex
2928import socket
3029import stat
31- import sys
3230import time
3331import typing
3432import warnings
@@ -238,7 +236,7 @@ def __init__(
238236 for proxy connection auth information is collected from SSHConfig
239237 if ssh_auth_map record is not available
240238
241- .. versionchanged:: 6.0.0 private_keys, auth and vebose became keyword-only arguments
239+ .. versionchanged:: 6.0.0 private_keys, auth and verbose became keyword-only arguments
242240 .. versionchanged:: 6.0.0 added optional ssh_config for ssh-proxy & low level connection parameters handling
243241 .. versionchanged:: 6.0.0 added optional ssh_auth_map for ssh proxy cases with authentication on each step
244242 .. versionchanged:: 6.0.0 added optional sock for manual proxy chain handling
@@ -263,7 +261,10 @@ def __init__(
263261
264262 # Save resolved hostname and port
265263 self .__hostname : str = config .hostname
266- self .__port : int = port if port is not None else config .port if config .port is not None else 22
264+ if port is not None :
265+ self .__port : int = port
266+ else :
267+ self .__port = config .port if config .port is not None else 22
267268
268269 # Store initial auth mapping
269270 self .__auth_mapping = ssh_auth .SSHAuthMapping (ssh_auth_map )
@@ -295,11 +296,11 @@ def __init__(
295296
296297 # Init super with host and real port and username
297298 mod_name = "exec_helpers" if self .__module__ .startswith ("exec_helpers" ) else self .__module__
298- log_username : typing . Optional [ str ] = self . __get_user_for_log ( real_auth )
299+ log_username : str = real_auth . username if real_auth . username is not None else getpass . getuser ( )
299300
300- super (SSHClientBase , self ).__init__ (
301+ super ().__init__ (
301302 logger = logging .getLogger (f"{ mod_name } .{ self .__class__ .__name__ } " ).getChild (
302- f"({ log_username } @{ host } :{ self .__port } )"
303+ f"({ log_username } @{ host } :{ self .port } )"
303304 )
304305 )
305306
@@ -315,21 +316,6 @@ def __init__(
315316
316317 self .__connect ()
317318
318- @staticmethod
319- def __get_user_for_log (real_auth : ssh_auth .SSHAuth ) -> typing .Optional [str ]: # pragma: no cover
320- if real_auth .username is not None :
321- return real_auth .username
322- # noinspection PyBroadException
323- try :
324- if sys .platform != "win32" :
325- import pwd # pylint: disable=import-outside-toplevel,import-error
326-
327- uid : int = os .getuid () # pylint: disable=no-member
328- return pwd .getpwuid (uid ).pw_name # Correct for not windows only
329- return getpass .getuser ()
330- except Exception :
331- return None
332-
333319 def __rebuild_ssh_config (self ) -> None :
334320 """Rebuild main ssh config from available information."""
335321 self .__ssh_config [self .hostname ] = self .__ssh_config [self .hostname ].overridden_by (
@@ -1116,7 +1102,10 @@ def _get_proxy_channel(self, port: typing.Optional[int], ssh_config: SSHConfig,)
11161102
11171103 .. versionadded:: 6.0.0
11181104 """
1119- dest_port : int = port if port is not None else ssh_config .port if ssh_config .port is not None else 22
1105+ if port is not None :
1106+ dest_port : int = port
1107+ else :
1108+ dest_port = ssh_config .port if ssh_config .port is not None else 22
11201109
11211110 return self ._ssh .get_transport ().open_channel (
11221111 kind = "direct-tcpip" , dest_addr = (ssh_config .hostname , dest_port ), src_addr = (self .hostname , 0 )
@@ -1257,8 +1246,7 @@ def execute_through_host(
12571246
12581247 if target_port is not None : # pragma: no cover
12591248 warnings .warn (
1260- f'"target_port" argument was renamed to "port". '
1261- f"Old version will be droppped in the next major release." ,
1249+ f'"target_port" argument was renamed to "port". Old version will be dropped in the next major release.' ,
12621250 DeprecationWarning ,
12631251 )
12641252 port = target_port
@@ -1343,13 +1331,8 @@ def get_result(remote: "SSHClientBase") -> exec_result.ExecResult:
13431331 """
13441332 # pylint: disable=protected-access
13451333 cmd_for_log : str = remote ._mask_command (cmd = command , log_mask_re = log_mask_re )
1334+ remote ._log_command_execute (command = command , log_mask_re = log_mask_re , log_level = log_level , ** kwargs )
13461335
1347- target_path : typing .Optional [str ] = kwargs .get ("chroot_path" , remote ._chroot_path )
1348- chroot_info : str = "" if not target_path or target_path == "/" else f" (with chroot to: { target_path !r} )"
1349-
1350- remote .logger .log (
1351- level = log_level , msg = f"Executing command{ chroot_info } :\n { cmd_for_log !r} \n " ,
1352- )
13531336 async_result : SshExecuteAsyncResult = remote ._execute_async (
13541337 command ,
13551338 stdin = stdin ,
0 commit comments