«
»

SSH Tunnels Headaches

October 20th, 2005 by George Notaras

I’m writing a VNC mini howto and I got stuck with something. I wanted to do the following with a single command:

  • create the SSH tunnel (local port forwarding)
  • execute vncviewer on the local machine
  • have the SSH tunnel to be automatically closed at the time vncviewer was closed


After about one hour of trial and error and man page reading, I ended up with a very useful line of code:

ssh -f -L 25930:127.0.0.1:5904 -C me@remote sleep 10; vncviewer 127.0.0.1:25930:4

What this does is:

  • forwards local port 25930 to remote port 5904
  • forks the ssh session to the background executing a sleep command on the remote machine
  • executes vncviewer so that it connects to the vnc server through the tunnel

What I badly needed was a way to auto-close the tunnel at the time vncviewer was closed. This seems to do it. The sleep command keeps the tunnel open for ten seconds. After this period of time it’s closed, unless vncviewer or some other application uses it. In this case, it is closed at the time this application stops using it. Pretty fu..ing cool!! I have wasted all my head’s juice until I managed to figure this out…

The SSH Tunnels Headaches by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Terms and conditions beyond the scope of this license may be available at www.g-loaded.eu.

Related Articles

Tags: , , , , , ,

Bookmark and Share

One Response to “SSH Tunnels Headaches”

  1. agorf Says :

    I needed this too. Thanks. :-)

Comments are automatically disabled after a certain period of time. Further discussion about the published content is still possible though in the G-Loaded Forums.