I am running various services on an Ubuntu virtual machine running on Azure, and I would like to access some of the services remotely, even though the ports are not open publicly. To do so I have set up a script to do Local Port Forwarding using SSH.
Usually this works by initiaing a connection to the remote host, execute sleep 10
, and establish a connection on the forwarded portbefore the sleep command finishes. This works well when using a service that keeps the connection alive, such as remote SMB shares.
But I have a problem with accessing web services, as the connection closes after some time - from 10-90 seconds after establishing the connection.
To reconnect the connection when it closes I hacked together this little script based on this SO thread.
while true; do { \
while true; do \
echo echo ping; sleep 10; \
done } | ssh -f myapp.cloudapp.net -F .ssh/myapp.cloudapp.net_config \
-o ExitOnForwardFailure=yes sleep 10; echo "$(date) I died";
sleep 1;
done
Unfortunately this "fix" is somewhat flakey, and the connection drops quite often, so the browser hangs on every fifth request. It works, but is far from perfect, so I would like a better approach. Perhaps there could be some kind of script doing long polling or the like? Anyone that has solved this issue?