I am trying to tunnel to my database using python, but crashes with a warning:
"10.9.8.5", port 5433 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
My python settings:
`
with SSHTunnelForwarder(
('10.132.230.2', 22),
ssh_username="<usrnm>",
ssh_password="<psswrd>",
remote_bind_address=('10.9.8.5', 5433)) as server:
server.start()
print ("server connected")
params = {
'database': "<dbnm>",
'user': "postgres",
'password': "<dbpsswrd>",
'host': '10.9.8.5',
'port': 5433
}
conn = psycopg2.connect(**params)
curs = conn.cursor()
Everything below happened in 10.9.8.5 I tried to edit postgres configs:
i restarted postgres
but it didn't help
ok, after searching for more, I came across the fact that there may be a problem in the firewall i allowed port 5433
then i restarted server but i still get this message
CodePudding user response:
You're telling your database client to connect directly to 10.9.8.5:5433. That's not how tunneling works.
The SSHTunnelForwarder
opens a port on your local machine, which it then forwards to the given remote_bind_address
through the intermediate ssh server. It doesn't let you magically access the remote server under its original IP address. So you need to use localhost
or 127.0.0.1
when connecting to the database server, and use the right port number.
By default, SSHTunnelForwarder
chooses a random available port on the local host; the chosen port number is revealed afterwards through the local_bind_port
property. However, by default it opens this port to other hosts as well (0.0.0.0). This is not needed, so it's better to be explicit and bind only to localhost a.k.a. 127.0.0.1
; you can do this with the local_bind_address
argument.
Putting all of that together:
with SSHTunnelForwarder(
('10.132.230.2', 22),
ssh_username="<usrnm>",
ssh_password="<psswrd>",
remote_bind_address=('10.9.8.5', 5433),
local_bind_address=('127.0.0.1',)) as server: # Open port on localhost
server.start()
print ("server connected")
params = {
'database': "<dbnm>",
'user': "postgres",
'password': "<dbpsswrd>",
'host': '127.0.0.1', # Connect to localhost...
'port': server.local_bind_port, # ... on the chosen port
}
conn = psycopg2.connect(**params)
curs = conn.cursor()