1- import logging
1+ import getpass
22import os
3+ import logging
4+ import platform
35import subprocess
46import tempfile
5- import platform
67
78# we support both pg8000 and psycopg2
89try :
@@ -52,7 +53,8 @@ def __init__(self, conn_params: ConnectionParams):
5253 if self .port :
5354 self .ssh_args += ["-p" , self .port ]
5455 self .remote = True
55- self .username = conn_params .username or self .get_user ()
56+ self .username = conn_params .username or getpass .getuser ()
57+ self .ssh_dest = f"{ self .username } @{ self .host } " if conn_params .username else self .host
5658 self .add_known_host (self .host )
5759 self .tunnel_process = None
5860
@@ -97,9 +99,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
9799 """
98100 ssh_cmd = []
99101 if isinstance (cmd , str ):
100- ssh_cmd = ['ssh' , f" { self .username } @ { self . host } " ] + self .ssh_args + [cmd ]
102+ ssh_cmd = ['ssh' , self .ssh_dest ] + self .ssh_args + [cmd ]
101103 elif isinstance (cmd , list ):
102- ssh_cmd = ['ssh' , f" { self .username } @ { self . host } " ] + self .ssh_args + cmd
104+ ssh_cmd = ['ssh' , self .ssh_dest ] + self .ssh_args + cmd
103105 process = subprocess .Popen (ssh_cmd , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
104106 if get_process :
105107 return process
@@ -174,10 +176,6 @@ def set_env(self, var_name: str, var_val: str):
174176 """
175177 return self .exec_command ("export {}={}" .format (var_name , var_val ))
176178
177- # Get environment variables
178- def get_user (self ):
179- return self .exec_command ("echo $USER" , encoding = get_default_encoding ()).strip ()
180-
181179 def get_name (self ):
182180 cmd = 'python3 -c "import os; print(os.name)"'
183181 return self .exec_command (cmd , encoding = get_default_encoding ()).strip ()
@@ -248,9 +246,9 @@ def mkdtemp(self, prefix=None):
248246 - prefix (str): The prefix of the temporary directory name.
249247 """
250248 if prefix :
251- command = ["ssh" ] + self .ssh_args + [f" { self .username } @ { self . host } " , f"mktemp -d { prefix } XXXXX" ]
249+ command = ["ssh" ] + self .ssh_args + [self .ssh_dest , f"mktemp -d { prefix } XXXXX" ]
252250 else :
253- command = ["ssh" ] + self .ssh_args + [f" { self .username } @ { self . host } " , "mktemp -d" ]
251+ command = ["ssh" ] + self .ssh_args + [self .ssh_dest , "mktemp -d" ]
254252
255253 result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
256254
@@ -296,7 +294,7 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
296294 # For scp the port is specified by a "-P" option
297295 scp_args = ['-P' if x == '-p' else x for x in self .ssh_args ]
298296 if not truncate :
299- scp_cmd = ['scp' ] + scp_args + [f"{ self .username } @ { self . host } :{ filename } " , tmp_file .name ]
297+ scp_cmd = ['scp' ] + scp_args + [f"{ self .ssh_dest } :{ filename } " , tmp_file .name ]
300298 subprocess .run (scp_cmd , check = False ) # The file might not exist yet
301299 tmp_file .seek (0 , os .SEEK_END )
302300
@@ -312,11 +310,11 @@ def write(self, filename, data, truncate=False, binary=False, read_and_write=Fal
312310 tmp_file .write (data )
313311
314312 tmp_file .flush ()
315- scp_cmd = ['scp' ] + scp_args + [tmp_file .name , f"{ self .username } @ { self . host } :{ filename } " ]
313+ scp_cmd = ['scp' ] + scp_args + [tmp_file .name , f"{ self .ssh_dest } :{ filename } " ]
316314 subprocess .run (scp_cmd , check = True )
317315
318316 remote_directory = os .path .dirname (filename )
319- mkdir_cmd = ['ssh' ] + self .ssh_args + [f" { self .username } @ { self . host } " , f"mkdir -p { remote_directory } " ]
317+ mkdir_cmd = ['ssh' ] + self .ssh_args + [self .ssh_dest , f"mkdir -p { remote_directory } " ]
320318 subprocess .run (mkdir_cmd , check = True )
321319
322320 os .remove (tmp_file .name )
@@ -381,7 +379,7 @@ def get_pid(self):
381379 return int (self .exec_command ("echo $$" , encoding = get_default_encoding ()))
382380
383381 def get_process_children (self , pid ):
384- command = ["ssh" ] + self .ssh_args + [f" { self .username } @ { self . host } " , f"pgrep -P { pid } " ]
382+ command = ["ssh" ] + self .ssh_args + [self .ssh_dest , f"pgrep -P { pid } " ]
385383
386384 result = subprocess .run (command , stdout = subprocess .PIPE , stderr = subprocess .PIPE , text = True )
387385
0 commit comments