pahaz / sshtunnel

SSH tunnels to remote server.
MIT License
1.23k stars 184 forks source link

Add new ports to existing tunnel? #245

Open mdavis-xyz opened 3 years ago

mdavis-xyz commented 3 years ago

I'm trying to port-forward FTP over SSH (not the same as SFTP). This involves port forwarding unpredictable ports. e.g. I forward port 5678 now to download the list of files on the FTP server, then I request to download a file and that means I now need to forward 5679, and then for the next file 5670. At the moment I'm able to do it by creating a whole new SSH tunnel for every new port I need to forward. But that's quite slow, and also results in the SSH server throttling me.

Is it sufficient to just call _make_ssh_forward_server(rem, loc) to forward a new port reusing the existing SSH connection?

Does anyone else have a similar use case? Is it worth writing a public method to do this (i.e. stable and doesn't start with _)?

mdavis-xyz commented 3 years ago

The answer is no, that function is not sufficient. That creates the tunnel, but then you need to start it separately. I can't figure out how to do that. This code didn't seem sufficient:

_srv = self._server_list[0]
thread = threading.Thread(
    target=self._serve_forever_wrapper,
    args=(_srv, ),
    name='Srv-{0}'.format(address_to_str(_srv.local_port))
)
thread.daemon = self.daemon_forward_servers
thread.start()
self._check_tunnel(_srv)