@@ -45,6 +45,10 @@ def __init__(self, conn_params: ConnectionParams):
45
45
self .conn_params = conn_params
46
46
self .host = conn_params .host
47
47
self .ssh_key = conn_params .ssh_key
48
+ if self .ssh_key :
49
+ self .ssh_cmd = ["-i" , self .ssh_key ]
50
+ else :
51
+ self .ssh_cmd = []
48
52
self .remote = True
49
53
self .username = conn_params .username or self .get_user ()
50
54
self .add_known_host (self .host )
@@ -91,9 +95,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
91
95
"""
92
96
ssh_cmd = []
93
97
if isinstance (cmd , str ):
94
- ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " , '-i' , self .ssh_key , cmd ]
98
+ ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self .ssh_cmd + [ cmd ]
95
99
elif isinstance (cmd , list ):
96
- ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " , '-i' , self .ssh_key ] + cmd
100
+ ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self .ssh_cmd + cmd
97
101
process = subprocess .Popen (ssh_cmd , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
98
102
if get_process :
99
103
return process
@@ -242,9 +246,9 @@ def mkdtemp(self, prefix=None):
242
246
- prefix (str): The prefix of the temporary directory name.
243
247
"""
244
248
if prefix :
245
- command = ["ssh" , "-i" , self .ssh_key , f"{ self .username } @{ self .host } " , f"mktemp -d { prefix } XXXXX" ]
249
+ command = ["ssh" ] + self .ssh_cmd + [ f"{ self .username } @{ self .host } " , f"mktemp -d { prefix } XXXXX" ]
246
250
else :
247
- command = ["ssh" , "-i" , self .ssh_key , f"{ self .username } @{ self .host } " , "mktemp -d" ]
251
+ command = ["ssh" ] + self .ssh_cmd + [ f"{ self .username } @{ self .host } " , "mktemp -d" ]
248
252
249
253
result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
250
254
@@ -288,7 +292,7 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
288
292
289
293
with tempfile .NamedTemporaryFile (mode = mode , delete = False ) as tmp_file :
290
294
if not truncate :
291
- scp_cmd = ['scp' , '-i' , self .ssh_key , f"{ self .username } @{ self .host } :{ filename } " , tmp_file .name ]
295
+ scp_cmd = ['scp' ] + self .ssh_cmd + [ f"{ self .username } @{ self .host } :{ filename } " , tmp_file .name ]
292
296
subprocess .run (scp_cmd , check = False ) # The file might not exist yet
293
297
tmp_file .seek (0 , os .SEEK_END )
294
298
@@ -304,12 +308,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
304
308
tmp_file .write (data )
305
309
306
310
tmp_file .flush ()
307
-
308
- scp_cmd = ['scp' , '-i' , self .ssh_key , tmp_file .name , f"{ self .username } @{ self .host } :{ filename } " ]
311
+ scp_cmd = ['scp' ] + self .ssh_cmd + [tmp_file .name , f"{ self .username } @{ self .host } :{ filename } " ]
309
312
subprocess .run (scp_cmd , check = True )
310
313
311
314
remote_directory = os .path .dirname (filename )
312
- mkdir_cmd = ['ssh' , '-i' , self .ssh_key , f"{ self .username } @{ self .host } " , f"mkdir -p { remote_directory } " ]
315
+ mkdir_cmd = ['ssh' ] + self .ssh_cmd + [ f"{ self .username } @{ self .host } " , f"mkdir -p { remote_directory } " ]
313
316
subprocess .run (mkdir_cmd , check = True )
314
317
315
318
os .remove (tmp_file .name )
@@ -374,7 +377,7 @@ def get_pid(self):
374
377
return int (self .exec_command ("echo $$" , encoding = get_default_encoding ()))
375
378
376
379
def get_process_children (self , pid ):
377
- command = ["ssh" , "-i" , self .ssh_key , f"{ self .username } @{ self .host } " , f"pgrep -P { pid } " ]
380
+ command = ["ssh" ] + self .ssh_cmd + [ f"{ self .username } @{ self .host } " , f"pgrep -P { pid } " ]
378
381
379
382
result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
380
383
0 commit comments