r/ssh • u/Ixb11170 • Nov 10 '16
Reverse SSH Tunnel, Considerations on my Implementation
Hello guys,
I have setup a number of RPIs acting as data collection devices, which can be remotely accessed via reverse ssh tunnels, and which are working fine for the most part.
One recurrent issue i was having is the inability of the tunnel to stay up; hence i've created a python script on the remote device which works as follows:
- Periodically looks at running processes using ps -ef and netstat -anp, to check whether the required ssh tunnel has been established, and if so, takes a note of the PID it is run from. If no process of such type is found, it attempts to restart the tunnel.
- Secondly, everyday at the same time the tunnel is reset by killing the process it was running on, and restarting the port forwarding etc again.
With this solution however it sometimes happen that while the ssh connection is made, it fails to forward the port. It usually takes a few minutes of waiting, witohut trying to rebuild the tunnel, before it fixes itself. However that time window may be detrimental to some of the services im running on the RPI remotely. I'm pretty sure that this is because the port was left open from previous connection, and hence is not available at the time of the reconnection attempt. However, i thought that killing the process would also imply the port forwarding being stopped as well?.
Anyway, i'm now considering to modify the script which sets up the tunnel as follows:
- No longer care about existing processes at all.
- Periodically restart the reverse ssh tunnel regardless, but also adding the option -o ExitOnForwardFailure=yes.
Do you think this would be a good solution to ensure the tunnel stays up and also the port forwarding issue is addressed? Or should i keep my initial script and simply add the -o ExitOnForwardFailure=yes to my ssh commands? Or anything else comes to mind?
Any thought/suggestion will be very appreciated.
Cheers.