I have a box sat behind a firewall, and when its powered on (or internet connection resumed), I want it to connect to a server, allowing me to do a reverse SSH connection back to it.
I'm currently using:
ssh "$USER@$HOST" "tunnel.port.sh '$PORT' '$HOSTNAME'"; ssh -N -R $PORT:localhost:222 -o ServerAliveInterval=30 "$USER@$HOST";
$PORT is randomly assigned... because I want to avoid the "address already in use" issue, but am aware that a
0 would be "dynamically allocated on the server and reported to the client at run time" (not sure how to obtain that though).
But where it is randomly assigned, I need to connect to the box first, to run "tunnel.port.sh", which records what that
$PORT number is.
Obviously I would like a better (less hacky) solution.
I've looked at
autossh, but thats more to keep the connection alive.
And for extra complication, neither box should trust each other... so when it does ssh in via the "$USER" account, its put in a restricted shell (rbash), and only able to run the "tunnel.port.sh" script, or keep the connection alive... and likewise connections back still require authentication... so I don't think a VPN would work.