SSH automatic reconnect on timeout
by Mostafa on Mar.15, 2006, under How To ..., Linux, Software
There may already be some builtin option that allows a client to reconnect when a timeout occurs but I was too lazy to look through the man pages. So I came up with line of bash commands that will do just that:
$ while [ 1 ]; do ssh user@host.domain; sleep 120; done
This will keep reconnecting to the host 120 seconds after a connection drops out for whatever reason. This is specially handy to make sure that a remote tunnel stays open. Right now I use it to reach a single host (A,http) on a remote private network from home (B) via another machine in the private network (C) and an intermediate SSH server (D):
-
[user@C ~]$ while [ 1 ]; do ssh -R someport:A:80 user@D; sleep 120; done
-
[user@B ~]$ ssh -L 80:localhost:someport user@D
-
[root@B ~]# echo "127.0.0.1 A" >> /etc/hosts
Now it is possible to type “http://A” and visit the site from B as easily as from within the remote private network.
November 1st, 2008 on 5:21 pm
hi, i’ve found your howto via google, but find another post which told me to install autossh 😉
this is a very “simple” but most useful prog.: http://www.harding.motd.ca/autossh/
bests, thx, PasiC
November 1st, 2008 on 7:01 pm
Thanks for the pointer PasiC!
January 27th, 2013 on 12:02 pm
what is the difference in terms of speed or functionality between the “for loop” and autossh?
thanks.
January 27th, 2013 on 12:49 pm
Never got around to trying autossh since I haven’t actually had the need to do anything like this for long while.
If you decide to try it out, please consider posting your findings.