I'm trying to start an ssh tunnel from A to B and have it run despite things like: period network/wifi drops on A and remote server reboot on B. My ssh tunnel starts using upstart script on A with event start on (net-device-up IFACE=eth0)

I've found autossh which is supposed to handle these kinds of things, but had some trouble getting it to work. The upstart executes:

autossh -M 0 -2qTN -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2" -L 5678:somehost:5678 user@B

However when I log into B and kill -9 that tunnel session, autossh just exits with "Connection to B closed by remote host." That's not what I expected autossh to do.

Any advice on how to set this up? Any GUI service monitoring utilities out there that essentially display a green light if a service is up?



Maybe a combination with screen will help. For auto reconnect you will need a key authentification too.

I created an alias: alias sshtunnel='TERM=xterm ssh servername -a -x -t /opt/local/bin/screen -xRR -A -e^Zz -U -O -S tunnelToServer' Screen ensures that you terminal session is alive.

Last step is creating a script which checks the host and starts the alias again: $ while :; do while ! ping -c1 -t1 server >/dev/null; do sleep 10; done; sshtunnel; echo Connection to server dropped at $(date); sleep 10; done

That should survive all network drops. You can play around with the sleep times too.

If you call screen -ls you will find your screen session with the name "tunnelToServer".

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.