Random Wisdom

SSH automatic reconnect on timeout

by 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):

  1. [user@C ~]$ while [ 1 ]; do ssh -R someport:A:80 user@D; sleep 120; done
  2. [user@B ~]$ ssh -L 80:localhost:someport user@D
  3. [root@B ~]# echo "    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.


4 Comments for this entry

Leave a Reply