@@ -44,11 +44,13 @@ def __init__(self, conn_params: ConnectionParams):
4444super ().__init__ (conn_params .username )
4545self .conn_params = conn_params
4646self .host = conn_params .host
47+ self .port = conn_params .port
4748self .ssh_key = conn_params .ssh_key
49+ self .ssh_args = []
4850if 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 ]
5254self .remote = True
5355self .username = conn_params .username or self .get_user ()
5456self .add_known_host (self .host )
@@ -95,9 +97,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
9597 """
9698ssh_cmd = []
9799if 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 ]
99101elif 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
101103process = subprocess .Popen (ssh_cmd ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE )
102104if get_process :
103105return process
@@ -246,9 +248,9 @@ def mkdtemp(self, prefix=None):
246248 - prefix (str): The prefix of the temporary directory name.
247249 """
248250if 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" ]
250252else :
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" ]
252254
253255result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
254256
@@ -291,8 +293,10 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
291293mode = "r+b" if binary else "r+"
292294
293295with 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 ]
294298if 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 ]
296300subprocess .run (scp_cmd ,check = False )# The file might not exist yet
297301tmp_file .seek (0 ,os .SEEK_END )
298302
@@ -308,11 +312,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
308312tmp_file .write (data )
309313
310314tmp_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 } " ]
312316subprocess .run (scp_cmd ,check = True )
313317
314318remote_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 } " ]
316320subprocess .run (mkdir_cmd ,check = True )
317321
318322os .remove (tmp_file .name )
@@ -377,7 +381,7 @@ def get_pid(self):
377381return int (self .exec_command ("echo $$" ,encoding = get_default_encoding ()))
378382
379383def 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 } " ]
381385
382386result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
383387