Disable SSH Tunnel in Paramiko

We are trying to use the paramiko module to create on-demand SSH tunnels on arbitrary servers to query remote databases. We tried using the forward.py demo that comes with paramiko, but the big limitation is that there is no easy way to close the SSH tunnel and SSH connection after starting the socket server.

The limitation we have is that we cannot activate this from the shell and then kill the shell manually to stop the list. We need to open SSH connection, tunnel, do some actions through tunnel, close tunnel and close SSH connection in python.

I've seen references to the server.shutdown () method, but it's not clear how to properly implement it.

Any help would be greatly appreciated ...

+2


a source to share


1 answer


I'm not sure what you mean by "correct implementation" - you just have to keep track of the server object and call shutdown

on it whenever you want. The forward.py

server is not tracked because the last line forward_tunnel

is

ForwardServer(('', local_port), SubHander).serve_forever()

      

therefore, the server object can no longer be easily accessible. But you can just change that to, for example:



global theserver
theserver = ForwardServer(('', local_port), SubHander)
theserver.serve_forever()

      

and run the function forward_tunnel

in a separate thread so that the function main

will take control back (while it is serve_forever

running on that separate thread) and can be called theserver.shutdown()

whenever needed and needed.

+5


a source







All Articles