This blog has moved! Redirecting...
You should be automatically redirected. If not, visit http://scrolls.mafgani.net/ and update your bookmarks.

Wednesday, March 15, 2006

SSH automatic reconnect on timeout

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 "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.

2 Comments:

Blogger Unknown said...

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

6:21 PM  
Blogger DarkKnight said...

Thanks for the pointer PasiC!

8:01 PM  

Post a Comment

<< Home