Retry dropped ssh connections

6

2

I use ssh -L -g, but intermittently the connection is lost, thus:

$ ssh -L 8080:localhost:8080 example.com -g
Last login: Tue Apr 24 07:19:04 2012 from 203.0.113.123
Read from remote host example.com: Connection timed out
Connection to example.com closed.

Can anyone suggest a robust way to keep these connections alive, and reconnect it they are closed.

dukedave

Posted 2012-04-25T02:20:16.670

Reputation: 189

Answers

3

autossh appears to fulfill my requirement, from the readme:

autossh is a program to start a copy of ssh and monitor it, 
restarting it as necessary should it die or stop passing traffic.

Substituting the arguments given in the question:

autossh -M 0 -L 8080:localhost:8080 example.com -g

(the -M 0 is required to turn the monitoring loop off)

I was able to kill -9 the connection from example.com and see the connection re-established automatically by autossh:

local$ autossh -M 0 -L 8080:localhost:8080 example.com -g
Last login: Wed Apr 25 03:23:56 2012 from 203.0.113.123
example.com$

Execute kill -9 on PID of connection on example.com.

local$ Connection to example.com closed.
Last login: Wed Apr 25 03:34:01 2012 from 203.0.113.123
local$ 

dukedave

Posted 2012-04-25T02:20:16.670

Reputation: 189

8

You could try Mosh.

Lars Kotthoff

Posted 2012-04-25T02:20:16.670

Reputation: 1 447

mosh doesn't handle port forwarding – Elazar Leibovich – 2016-01-25T06:14:55.640

Damn, that looks awesome, I work on the train every day and I can't believe I never thought to look for a general solution to the problem. As you can probably see, I actually found autossh which appears to solve the specific problem I have, but I'll definitely check Mosh out. – dukedave – 2012-04-25T02:47:13.580