How create port forwarding using SSHtunnelForwarder?

Issue

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: https://github.com/pahaz/sshtunnel

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

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.



Solution

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?