@@ -44,11 +44,13 @@ def __init__(self, conn_params: ConnectionParams):
44
44
super ().__init__ (conn_params .username )
45
45
self .conn_params = conn_params
46
46
self .host = conn_params .host
47
+ self .port = conn_params .port
47
48
self .ssh_key = conn_params .ssh_key
49
+ self .ssh_args = []
48
50
if self .ssh_key :
49
- self .ssh_cmd = ["-i" , self .ssh_key ]
50
- else :
51
- self .ssh_cmd = []
51
+ self .ssh_args + = ["-i" , self .ssh_key ]
52
+ if self . port :
53
+ self .ssh_args + = ["-p" , self . port ]
52
54
self .remote = True
53
55
self .username = conn_params .username or self .get_user ()
54
56
self .add_known_host (self .host )
@@ -95,9 +97,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
95
97
"""
96
98
ssh_cmd = []
97
99
if isinstance (cmd , str ):
98
- ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self .ssh_cmd + [cmd ]
100
+ ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self .ssh_args + [cmd ]
99
101
elif isinstance (cmd , list ):
100
- ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self .ssh_cmd + cmd
102
+ ssh_cmd = ['ssh' , f"{ self .username } @{ self .host } " ] + self .ssh_args + cmd
101
103
process = subprocess .Popen (ssh_cmd , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
102
104
if get_process :
103
105
return process
@@ -246,9 +248,9 @@ def mkdtemp(self, prefix=None):
246
248
- prefix (str): The prefix of the temporary directory name.
247
249
"""
248
250
if prefix :
249
- command = ["ssh" ] + self .ssh_cmd + [f"{ self .username } @{ self .host } " , f"mktemp -d { prefix } XXXXX" ]
251
+ command = ["ssh" ] + self .ssh_args + [f"{ self .username } @{ self .host } " , f"mktemp -d { prefix } XXXXX" ]
250
252
else :
251
- command = ["ssh" ] + self .ssh_cmd + [f"{ self .username } @{ self .host } " , "mktemp -d" ]
253
+ command = ["ssh" ] + self .ssh_args + [f"{ self .username } @{ self .host } " , "mktemp -d" ]
252
254
253
255
result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
254
256
@@ -291,8 +293,10 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
291
293
mode = "r+b" if binary else "r+"
292
294
293
295
with tempfile .NamedTemporaryFile (mode = mode , delete = False ) as tmp_file :
296
+ # For scp the port is specified by a "-P" option
297
+ scp_args = ['-P' if x == '-p' else x for x in self .ssh_args ]
294
298
if not truncate :
295
- scp_cmd = ['scp' ] + self . ssh_cmd + [f"{ self .username } @{ self .host } :{ filename } " , tmp_file .name ]
299
+ scp_cmd = ['scp' ] + scp_args + [f"{ self .username } @{ self .host } :{ filename } " , tmp_file .name ]
296
300
subprocess .run (scp_cmd , check = False ) # The file might not exist yet
297
301
tmp_file .seek (0 , os .SEEK_END )
298
302
@@ -308,11 +312,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
308
312
tmp_file .write (data )
309
313
310
314
tmp_file .flush ()
311
- scp_cmd = ['scp' ] + self . ssh_cmd + [tmp_file .name , f"{ self .username } @{ self .host } :{ filename } " ]
315
+ scp_cmd = ['scp' ] + scp_args + [tmp_file .name , f"{ self .username } @{ self .host } :{ filename } " ]
312
316
subprocess .run (scp_cmd , check = True )
313
317
314
318
remote_directory = os .path .dirname (filename )
315
- mkdir_cmd = ['ssh' ] + self .ssh_cmd + [f"{ self .username } @{ self .host } " , f"mkdir -p { remote_directory } " ]
319
+ mkdir_cmd = ['ssh' ] + self .ssh_args + [f"{ self .username } @{ self .host } " , f"mkdir -p { remote_directory } " ]
316
320
subprocess .run (mkdir_cmd , check = True )
317
321
318
322
os .remove (tmp_file .name )
@@ -377,7 +381,7 @@ def get_pid(self):
377
381
return int (self .exec_command ("echo $$" , encoding = get_default_encoding ()))
378
382
379
383
def get_process_children (self , pid ):
380
- command = ["ssh" ] + self .ssh_cmd + [f"{ self .username } @{ self .host } " , f"pgrep -P { pid } " ]
384
+ command = ["ssh" ] + self .ssh_args + [f"{ self .username } @{ self .host } " , f"pgrep -P { pid } " ]
381
385
382
386
result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
383
387
0 commit comments