@@ -48,13 +48,14 @@ def __init__(self, conn_params: ConnectionParams):
48
48
super ().__init__ (conn_params .username )
49
49
self .conn_params = conn_params
50
50
self .host = conn_params .host
51
- self .ssh_key = conn_params .ssh_key
52
51
self .port = conn_params .port
52
+ self .ssh_key = conn_params .ssh_key
53
53
self .ssh_cmd = ["-o StrictHostKeyChecking=no" ]
54
+ self .ssh_args = []
54
55
if self .ssh_key :
55
- self .ssh_cmd += ["-i" ,self .ssh_key ]
56
+ self .ssh_args += ["-i" ,self .ssh_key ]
56
57
if self .port :
57
- self .ssh_cmd += ["-p" ,self .port ]
58
+ self .ssh_args += ["-p" ,self .port ]
58
59
self .remote = True
59
60
self .username = conn_params .username or self .get_user ()
60
61
self .tunnel_process = None
@@ -113,9 +114,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
113
114
"""
114
115
ssh_cmd = []
115
116
if 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 ]
117
118
elif 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
119
120
process = subprocess .Popen (ssh_cmd ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE )
120
121
if get_process :
121
122
return process
@@ -264,9 +265,9 @@ def mkdtemp(self, prefix=None):
264
265
- prefix (str): The prefix of the temporary directory name.
265
266
"""
266
267
if 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" ]
268
269
else :
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" ]
270
271
271
272
result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
272
273
@@ -309,11 +310,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
309
310
mode = "r+b" if binary else "r+"
310
311
311
312
with 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 ]
314
315
315
316
if 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 ]
317
318
subprocess .run (scp_cmd ,check = False )# The file might not exist yet
318
319
tmp_file .seek (0 ,os .SEEK_END )
319
320
@@ -329,11 +330,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
329
330
tmp_file .write (data )
330
331
331
332
tmp_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 } " ]
333
334
subprocess .run (scp_cmd ,check = True )
334
335
335
336
remote_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 } " ]
337
338
subprocess .run (mkdir_cmd ,check = True )
338
339
339
340
os .remove (tmp_file .name )
@@ -398,7 +399,7 @@ def get_pid(self):
398
399
return int (self .exec_command ("echo $$" ,encoding = get_default_encoding ()))
399
400
400
401
def 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 } " ]
402
403
403
404
result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
404
405