Reverse SSH Tunnel with Dynamic Port Allocation

Viewed 285

We have a system that implements reverse ssh tunnel to enable remote port forwarding.

This enables to expose devices on local network via a Public IP. However, we're achieving this by a static port allocation, i.e. We've mapped Serial No.s of devices to Ports and each time a request comes to our server with Serial No. , we forward it to the designated port.

Obviously this design isn't scalable and we want to replace it with dynamic port allocation, such that the server itself takes care of allocating the port and freeing it once the device goes offline.

It'd be great to have suggestions on how this can be achieved.

Thanks in anticipation and apologies for my lack of knowledge in case I didn't explain it better (I'm new to this)

1 Answers

This can be solved by using a ssh command script in ~/.ssh/authorized_keys on your server.

command="reverse_server.py" ssh-rsa <publicKey>

First, a client has to connect to the script and send it's own serial (in my case a use the MAC-adress of one NIC).
The server looks into a database and tell the assigned port number or creates a new one for an unknown serial.

In the next step, the client can connect, using the reported port number as reverse port.

Btw. You should add some logic to monitor and recreate the tunnel if it disconnects or get stuck.

Related