sshtunnel icon indicating copy to clipboard operation
sshtunnel copied to clipboard

ChannelException(2, 'Connect failed')

Open auroramariatumminello opened this issue 3 years ago • 1 comments

Hi everyone, I have encountered a problem with SSHTunnerlForwarder. In particular, I'm trying to connect to a MySQL database on a EC2 Instance on AWS. I have set the public and private IP addresses, username and key certificate to compose it. Then I have created a MySQL connection with the database settings (username, password, port and type of auth) as follows:

class MySQLStationManagerAWS:
    
    def __init__(self):
        try:
            with sshtunnel.SSHTunnelForwarder(('ec2-XXX-XXX-XXX-XXX.eu-central-1.compute.amazonaws.com',22),
                                            ssh_username='username',
                                            ssh_password=None,
                                            ssh_pkey='my_secret_key.pem',
                                            remote_bind_address=('ip-XXX-XXX-XXX-XXX.eu-central-1.compute.internal', 3306),) as tunnel:
                self.connection = mysql.connector.connect(
                    host="127.0.0.1",
                    user="username",
                    password="my_password",
                    port=tunnel.local_bind_port,
                    database="database_name",
                    auth_plugin='mysql_native_password')
                print("Connected successfully.")
        except InterfaceError:
            print("Lost connection to MySQL server")
    
    def disconnect(self):
        self.connection.close()

However, it looks like it returns me the following error:

2021-05-13 11:32:21,701| ERROR   | Secsh channel 0 open FAILED: Connection refused: Connect failed
2021-05-13 11:32:21,703| ERROR   | Could not establish connection from local ('127.0.0.1', 60966) to remote ('ip-XXX-XXX-XXX-XXX.eu-central-1.compute.internal', 3306) side of the tunnel: open new channel ssh error: ChannelException(2, 'Connect failed')

followed by an Interface error by MySQL that is suddenly disconnected (i.e. the exception handled in the first chunk code).

Does anybody have an idea of what I'm mistaking?

auroramariatumminello avatar May 13 '21 09:05 auroramariatumminello

Could you please add extra logging like so:

sshtunnel.DEFAULT_LOGLEVEL = 1
logging.basicConfig(
    format='%(asctime)s| %(levelname)-4.3s|%(threadName)10.9s/%(lineno)04d@%(module)-10.9s| %(message)s', level=1)
logger = logging.root

...

class MySQLStationManagerAWS:
    
    def __init__(self):
        try:
            with sshtunnel.SSHTunnelForwarder(('ec2-XXX-XXX-XXX-XXX.eu-central-1.compute.amazonaws.com',22),
                                            logger=logger,  # <---!
                                            ssh_username='username',
                                            ssh_password=None,
                                            ssh_pkey='my_secret_key.pem',
                                            remote_bind_address=('ip-XXX-XXX-XXX-XXX.eu-central-1.compute.internal', 3306),) as tunnel:
...

pahaz avatar May 12 '23 14:05 pahaz