@@ -48,13 +48,14 @@ def __init__(self, conn_params: ConnectionParams):
4848super ().__init__ (conn_params .username )
4949self .conn_params = conn_params
5050self .host = conn_params .host
51- self .ssh_key = conn_params .ssh_key
5251self .port = conn_params .port
52+ self .ssh_key = conn_params .ssh_key
5353self .ssh_cmd = ["-o StrictHostKeyChecking=no" ]
54+ self .ssh_args = []
5455if self .ssh_key :
55- self .ssh_cmd += ["-i" ,self .ssh_key ]
56+ self .ssh_args += ["-i" ,self .ssh_key ]
5657if self .port :
57- self .ssh_cmd += ["-p" ,self .port ]
58+ self .ssh_args += ["-p" ,self .port ]
5859self .remote = True
5960self .username = conn_params .username or self .get_user ()
6061self .tunnel_process = None
@@ -113,9 +114,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
113114 """
114115ssh_cmd = []
115116if isinstance (cmd ,str ):
116- ssh_cmd = ['ssh' ,f"{ self .username } @{ self .host } " ]+ self .ssh_cmd + [cmd ]
117+ ssh_cmd = ['ssh' ,f"{ self .username } @{ self .host } " ]+ self .ssh_args + [cmd ]
117118elif isinstance (cmd ,list ):
118- ssh_cmd = ['ssh' ,f"{ self .username } @{ self .host } " ]+ self .ssh_cmd + cmd
119+ ssh_cmd = ['ssh' ,f"{ self .username } @{ self .host } " ]+ self .ssh_args + cmd
119120process = subprocess .Popen (ssh_cmd ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE )
120121if get_process :
121122return process
@@ -264,9 +265,9 @@ def mkdtemp(self, prefix=None):
264265 - prefix (str): The prefix of the temporary directory name.
265266 """
266267if prefix :
267- command = ["ssh" + f"{ self .username } @{ self .host } " ] + self . ssh_cmd + [ f"mktemp -d{ prefix } XXXXX" ]
268+ command = ["ssh" ] + self . ssh_args + [ f"{ self .username } @{ self .host } " , f"mktemp -d{ prefix } XXXXX" ]
268269else :
269- command = ["ssh" , f"{ self .username } @{ self .host } " ] + self . ssh_cmd + [ "mktemp -d" ]
270+ command = ["ssh" ] + self . ssh_args + [ f"{ self .username } @{ self .host } " , "mktemp -d" ]
270271
271272result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
272273
@@ -309,11 +310,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
309310mode = "r+b" if binary else "r+"
310311
311312with tempfile .NamedTemporaryFile (mode = mode ,delete = False )as tmp_file :
312- #Because in scpwe set up port using -P option
313- scp_ssh_cmd = ['-P' if x == '-p' else x for x in self .ssh_cmd ]
313+ #For scpthe port is specified by a "-P" option
314+ scp_args = ['-P' if x == '-p' else x for x in self .ssh_args ]
314315
315316if not truncate :
316- scp_cmd = ['scp' ]+ scp_ssh_cmd + [f"{ self .username } @{ self .host } :{ filename } " ,tmp_file .name ]
317+ scp_cmd = ['scp' ]+ scp_args + [f"{ self .username } @{ self .host } :{ filename } " ,tmp_file .name ]
317318subprocess .run (scp_cmd ,check = False )# The file might not exist yet
318319tmp_file .seek (0 ,os .SEEK_END )
319320
@@ -329,11 +330,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
329330tmp_file .write (data )
330331
331332tmp_file .flush ()
332- scp_cmd = ['scp' ]+ scp_ssh_cmd + [tmp_file .name ,f"{ self .username } @{ self .host } :{ filename } " ]
333+ scp_cmd = ['scp' ]+ scp_args + [tmp_file .name ,f"{ self .username } @{ self .host } :{ filename } " ]
333334subprocess .run (scp_cmd ,check = True )
334335
335336remote_directory = os .path .dirname (filename )
336- mkdir_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self . ssh_cmd + [ f"mkdir -p{ remote_directory } " ]
337+ mkdir_cmd = ['ssh' ] + self . ssh_args + [ f"{ self .username } @{ self .host } " , f"mkdir -p{ remote_directory } " ]
337338subprocess .run (mkdir_cmd ,check = True )
338339
339340os .remove (tmp_file .name )
@@ -398,7 +399,7 @@ def get_pid(self):
398399return int (self .exec_command ("echo $$" ,encoding = get_default_encoding ()))
399400
400401def get_process_children (self ,pid ):
401- command = ["ssh" , f"{ self .username } @{ self .host } " ] + self . ssh_cmd + [ f"pgrep -P{ pid } " ]
402+ command = ["ssh" ] + self . ssh_args + [ f"{ self .username } @{ self .host } " , f"pgrep -P{ pid } " ]
402403
403404result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
404405