11import getpass
22import os
3- import socket
3+ import logging
4+ import platform
45import subprocess
56import tempfile
6- import platform
77
88# we support both pg8000 and psycopg2
99try :
@@ -55,35 +55,10 @@ def __init__(self, conn_params: ConnectionParams):
5555 self .remote = True
5656 self .username = conn_params .username or getpass .getuser ()
5757 self .ssh_dest = f"{ self .username } @{ self .host } " if conn_params .username else self .host
58- #self.add_known_host(self.host)
59- self .tunnel_process = None
60- self .tunnel_port = None
6158
6259 def __enter__ (self ):
6360 return self
6461
65- def __exit__ (self , exc_type , exc_val , exc_tb ):
66- self .close_ssh_tunnel ()
67-
68- @staticmethod
69- def is_port_open (host , port ):
70- with socket .socket (socket .AF_INET , socket .SOCK_STREAM ) as sock :
71- sock .settimeout (1 ) # Таймаут для попытки соединения
72- try :
73- sock .connect ((host , port ))
74- return True
75- except socket .error :
76- return False
77-
78- def close_ssh_tunnel (self ):
79- if self .tunnel_process :
80- self .tunnel_process .terminate ()
81- self .tunnel_process .wait ()
82- print ("SSH tunnel closed." )
83- del self .tunnel_process
84- else :
85- print ("No active tunnel to close." )
86-
8762 def exec_command (self , cmd , wait_exit = False , verbose = False , expect_error = False ,
8863 encoding = None , shell = True , text = False , input = None , stdin = None , stdout = None ,
8964 stderr = None , get_process = None , timeout = None ):
@@ -94,9 +69,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
9469 """
9570 ssh_cmd = []
9671 if isinstance (cmd , str ):
97- ssh_cmd = ['ssh' ] + self .ssh_args + [self . ssh_dest , cmd ]
72+ ssh_cmd = ['ssh' , self . ssh_dest ] + self .ssh_args + [cmd ]
9873 elif isinstance (cmd , list ):
99- ssh_cmd = ['ssh' ] + self .ssh_args + [ self .ssh_dest ] + cmd
74+ ssh_cmd = ['ssh' , self .ssh_dest ] + self .ssh_args + cmd
10075 process = subprocess .Popen (ssh_cmd , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
10176 if get_process :
10277 return process
0 commit comments