1
1
import getpass
2
2
import os
3
- import socket
3
+ import logging
4
+ import platform
4
5
import subprocess
5
6
import tempfile
6
- import platform
7
7
8
8
# we support both pg8000 and psycopg2
9
9
try :
@@ -55,35 +55,10 @@ def __init__(self, conn_params: ConnectionParams):
55
55
self .remote = True
56
56
self .username = conn_params .username or getpass .getuser ()
57
57
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
61
58
62
59
def __enter__ (self ):
63
60
return self
64
61
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
-
87
62
def exec_command (self , cmd , wait_exit = False , verbose = False , expect_error = False ,
88
63
encoding = None , shell = True , text = False , input = None , stdin = None , stdout = None ,
89
64
stderr = None , get_process = None , timeout = None ):
@@ -94,9 +69,9 @@ def exec_command(self, cmd, wait_exit=False, verbose=False, expect_error=False,
94
69
"""
95
70
ssh_cmd = []
96
71
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 ]
98
73
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
100
75
process = subprocess .Popen (ssh_cmd , stdin = subprocess .PIPE , stdout = subprocess .PIPE , stderr = subprocess .PIPE )
101
76
if get_process :
102
77
return process
0 commit comments