How create port forwarding using SSHtunnelForwarder?


This Content is from Stack Overflow. Question asked by Natan

I am trying to replicate: ssh -i [KEY] -L [FROM_PORT]:localhost:[TO_PORT] ubuntu@[REMOTE_SERVER_IP] in python and decided to use sshtunnel for it. The command given above works and I can connect to the remote Theia IDE, but I can’t figure out how I need to configure SSHtunnelForwarder to achieve the same from within python. I am probably misunderstanding its documentation:

    # I replaced the remote-ip with ``.
    # I replaced my ip with `987.65.43.21`.
    with SSHTunnelForwarder(
        ssh_address_or_host="",  # ip of remote server
        ssh_username="ubuntu",  # username of remote server
        ssh_bind_address= ("", 9191),  # where to listen locally
        remote_bind_address= ("", 8181)  # where to send info coming to 22
        # ssh-port 22 (default port)
        # localhost:9191 -> -> localhost:8181
        # which is basically 987.65.43.21:9191 -> ->
    ) as server:

But when I try to connect to http://localhost:9191/ I receive unable to connect; so the tunneling is probably broken. I added plenty of comments to the code to make it easier to spot my misunderstanding.


Check the Answers

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?