Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSH tunnels stop working after some time #261

Open
f1nality opened this issue Jul 28, 2022 · 12 comments
Open

SSH tunnels stop working after some time #261

f1nality opened this issue Jul 28, 2022 · 12 comments

Comments

@f1nality
Copy link

I faced strange behavior. I'm using the following code to set up SSH tunnels to connect to PostgreSQL

    from sshtunnel import SSHTunnelForwarder
    import paramiko

    private_key_str = conf.get('ssh_private_key').replace('\\n', '\n')
    private_key = paramiko.RSAKey.from_private_key(StringIO(private_key_str))

    server = SSHTunnelForwarder(
        ssh_address_or_host=(conf.get('ssh_host'), int(conf.get('ssh_port'))),
        ssh_username=conf.get('ssh_user'),
        ssh_pkey=private_key,
        remote_bind_address=(conf.get('host'), int(conf.get('port'))),
        logger=logger
    )
    server.start()

When connection is made everything works ok, but after some time (~10 hours) i'm getting PSQL error:

psycopg2.OperationalError: server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.

netstat -lntu shows opened ports, but making telnet returns error.

bash-5.0# netstat -lntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       
tcp        0      0 0.0.0.0:36877           0.0.0.0:*               LISTEN       
bash-5.0# telnet localhost:36877
Connected to localhost:36877
Connection closed by foreign host

All new SSH tunnels started afterwards - also returns errors right after start (and somehow they create two open ports).
Only application restart solves the problem.

Starting SSH tunnel manually through ssh -L localhost:1111:DB_HOST:DB_PORT -i KEY BASTION_USER@BASTION_HOST works fine, such tunnels work and don't return telnet error.

@lucasgherculano
Copy link

did you tried the keepalive parameter?

@f1nality
Copy link
Author

@lucasgherculano Do you mean SSH client or server? Tried adding SSH bastion server parameters, but need time to test if it helped:

ClientAliveInterval 300
ClientAliveCountMax 2

I thought sshtunnel should implement keepalive on its side.

@f1nality
Copy link
Author

keepalive didn't work

@f1nality
Copy link
Author

f1nality commented Aug 2, 2022

Manually creating SSH tunnel with "ssh" command works ok

@lucaslcode
Copy link

@f1nality also encountering this. Did you ever find a solution?

@matthew-k2001
Copy link

Hi @f1nality , any idea how you fixed this?

@f1nality
Copy link
Author

f1nality commented Mar 4, 2024

@lucaslcode @matthew-k2001 I've switched to using "ssh" command to create tunnels in the end :(

@matthew-k2001
Copy link

Thanks @f1nality for the quick reply, could you elaborate what you mean?

@f1nality
Copy link
Author

f1nality commented Mar 4, 2024

@matthew-k2001 i'm using subprocess.Popen to run command "ssh -N ...". You can check source code here:
https://github.com/jet-admin/jet-bridge/blob/master/packages/jet_bridge_base/jet_bridge_base/ssh_tunnel.py

@matthew-k2001
Copy link

Thanks @f1nality, my knowlegde might be lacking here so could you clarify why does that keep the connection. Is it similar to AutoSSH?

@f1nality
Copy link
Author

f1nality commented Mar 4, 2024

@matthew-k2001 this library sshtunnel implements SSH using paramiko and TCP sockets, "ssh" command is a much more widely used utility in linux distributives which has its own implementation. So it is actually different code, making the same. But looks like sshtunnel or paramiko (less likely) has some bug with keeping connections alive, but "ssh" command does not have this bug.

@matthew-k2001
Copy link

Thanks @f1nality that makes sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants