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 an 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.
Remote Side:
Our Demo Python Flask Application:
|
|
Run the server:
|
Client Side:
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:
|
|
Running our app:
|
So we have successfully established our ssh tunnel to our remote side, and able to access the network restricted port via the tunnel.