Sometimes we need to restrict access to a port, where a port should listen on localhost, but you want to access that port from a remote source. One secure way of doing that, is to establish a SSH Tunnel to the remote side, and forward to port via the SSH Tunnel.
Today we will setup a Flask Web Service on our Remote Server (Side B) which will be listening on
127.0.0.1:5000 and setup the SSH Tunnel with the
sshtunnel module in Python from our client side (Side A). Then we will make a GET request on our client side to the port that we are forwarding via the tunnel to our remote side.
Our Demo Python Flask Application:
1 2 3 4 5 6 7 8 9 10
Run the server:
From our client side we first need to install sshtunnel via pip:
Our code for our client that will establish the tunnel and do the GET request:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Running our app:
So we have sucessfully established our ssh tunnel to our remote side, and able to access the network restricted port via the tunnel.