@@ -51,6 +51,7 @@ def __init__(self, conn_params: ConnectionParams):
51
51
self .ssh_cmd = []
52
52
self .remote = True
53
53
self .username = conn_params .username or self .get_user ()
54
+ self .ssh_dest = f"{ self .username } @{ self .host } " if self .username else "{self.host}"
54
55
self .add_known_host (self .host )
55
56
self .tunnel_process = None
56
57
@@ -95,9 +96,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
95
96
"""
96
97
ssh_cmd = []
97
98
if isinstance (cmd ,str ):
98
- ssh_cmd = ['ssh' ,f" { self .username } @ { self . host } " ]+ self .ssh_cmd + [cmd ]
99
+ ssh_cmd = ['ssh' ,self .ssh_dest ]+ self .ssh_cmd + [cmd ]
99
100
elif isinstance (cmd ,list ):
100
- ssh_cmd = ['ssh' ,f" { self .username } @ { self . host } " ]+ self .ssh_cmd + cmd
101
+ ssh_cmd = ['ssh' ,self .ssh_dest ]+ self .ssh_cmd + cmd
101
102
process = subprocess .Popen (ssh_cmd ,stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE )
102
103
if get_process :
103
104
return process
@@ -246,9 +247,9 @@ def mkdtemp(self, prefix=None):
246
247
- prefix (str): The prefix of the temporary directory name.
247
248
"""
248
249
if prefix :
249
- command = ["ssh" ]+ self .ssh_cmd + [f" { self .username } @ { self . host } " ,f"mktemp -d{ prefix } XXXXX" ]
250
+ command = ["ssh" ]+ self .ssh_cmd + [self .ssh_dest ,f"mktemp -d{ prefix } XXXXX" ]
250
251
else :
251
- command = ["ssh" ]+ self .ssh_cmd + [f" { self .username } @ { self . host } " ,"mktemp -d" ]
252
+ command = ["ssh" ]+ self .ssh_cmd + [self .ssh_dest ,"mktemp -d" ]
252
253
253
254
result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
254
255
@@ -292,7 +293,7 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
292
293
293
294
with tempfile .NamedTemporaryFile (mode = mode ,delete = False )as tmp_file :
294
295
if not truncate :
295
- scp_cmd = ['scp' ]+ self .ssh_cmd + [f"{ self .username } @ { self . host } :{ filename } " ,tmp_file .name ]
296
+ scp_cmd = ['scp' ]+ self .ssh_cmd + [f"{ self .ssh_dest } :{ filename } " ,tmp_file .name ]
296
297
subprocess .run (scp_cmd ,check = False )# The file might not exist yet
297
298
tmp_file .seek (0 ,os .SEEK_END )
298
299
@@ -308,11 +309,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
308
309
tmp_file .write (data )
309
310
310
311
tmp_file .flush ()
311
- scp_cmd = ['scp' ]+ self .ssh_cmd + [tmp_file .name ,f"{ self .username } @ { self . host } :{ filename } " ]
312
+ scp_cmd = ['scp' ]+ self .ssh_cmd + [tmp_file .name ,f"{ self .ssh_dest } :{ filename } " ]
312
313
subprocess .run (scp_cmd ,check = True )
313
314
314
315
remote_directory = os .path .dirname (filename )
315
- mkdir_cmd = ['ssh' ]+ self .ssh_cmd + [f" { self .username } @ { self . host } " ,f"mkdir -p{ remote_directory } " ]
316
+ mkdir_cmd = ['ssh' ]+ self .ssh_cmd + [self .ssh_dest ,f"mkdir -p{ remote_directory } " ]
316
317
subprocess .run (mkdir_cmd ,check = True )
317
318
318
319
os .remove (tmp_file .name )
@@ -377,7 +378,7 @@ def get_pid(self):
377
378
return int (self .exec_command ("echo $$" ,encoding = get_default_encoding ()))
378
379
379
380
def get_process_children (self ,pid ):
380
- command = ["ssh" ]+ self .ssh_cmd + [f" { self .username } @ { self . host } " ,f"pgrep -P{ pid } " ]
381
+ command = ["ssh" ]+ self .ssh_cmd + [self .ssh_dest ,f"pgrep -P{ pid } " ]
381
382
382
383
result = subprocess .run (command ,stdout = subprocess .PIPE ,stderr = subprocess .PIPE ,text = True )
383
384