I am using cx_Oracle version 6.2.1 against Oracle 12.1.0.2. The database is behind a bastion server, so I need to tunnel through the SSH port and the bastion server.
If I establish the tunnel manually, using the terminal, using cx_Oracle works just fine:
dsn_tns = cx_Oracle.makedsn(localhost, db_port, SID)
conn = cx_Oracle.connect(db_user, db_password, dsn_tns)
print(conn.version)
conn.close()
If I establish an SSH connection via the sshtunnel Python module, and leave it open (break while in debug mode on print('foo') below, I can connect to the database using SQLDeveloper.
with SSHTunnelForwarder(
(bastion_server, 22),
ssh_username="admin",
ssh_pkey=ssh_pkey,
remote_bind_address=(remote_bind_address, db_port),
local_bind_address=('0.0.0.0', db_port)
) as tunnel:
print('foo')
However, if I try to do both via code, when I try to do anything with the connection object, it times out.
with SSHTunnelForwarder(
(bastion_server, 22),
ssh_username="admin",
ssh_pkey=ssh_pkey,
remote_bind_address=(remote_bind_address, db_port),
local_bind_address=('0.0.0.0', db_port)
) as tunnel:
dsn_tns = cx_Oracle.makedsn(localhost, db_port, SID)
conn = cx_Oracle.connect(db_user, db_password, dsn_tns)
print(conn.version)
conn.close()
In the above example, the script hangs when trying to execute print(conn.version)
. If I stop before reaching that line, and try to look at the conn variable, I get this error after a little while of it waiting: "Unable to display children:Timeout waiting for response on 110".
Any idea why this does not work? Is there something mutually exclusive about cx_Oracle and sshtunnel? I've searched the web for hours for an example, and I haven't found a thing. Even pages that I have found showing concrete examples of connecting to databases, will only show cx_Oracle examples without ssh tunneling (whereas the other examples weave sshtunnel into the example).
Thanks for reading this and looking into the issue!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4