'How to connect over sshtunnel to remote sql server with "2 jumps" in python

after days of research and fails with this problem.

Context:

I need to connect remotely from a python script to a remote database and I have the following restrictions:

Linux Machine (Local) -> Windows 10 Machine(ODBC conn) -> Machine with SQL Server

The Windows machine has a connection via ODBC with the machine that contains the database and unfortunately I can not connect directly to the DB.

My question is the following: is it possible through python using an sshtunnel or something similar to create such a connection?

I have used this code, but it only works for a direct connection

with SSHTunnelForwarder(
            (ssh_host, ssh_port),
            ssh_username=ssh_user,
            ssh_pkey=mypkey,
            remote_bind_address=(sql_hostname, sql_port)) as tunnel:
        conn = connect(host='127.0.0.1', user=sql_username,
                passwd=sql_password, db=sql_main_database,
                port=tunnel.local_bind_port)
        data = pd.read_sql_query(query, conn)
        conn.close()

thansk for the help



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source