Re: How to kill port forwarding ssh when client exits
From: Simon Tatham (anakin_at_pobox.com)
Date: 08 Jan 2005 11:51:00 +0000 (GMT)
HK <firstname.lastname@example.org> wrote:
> with port forwarding like
> ssh -L 5432:host.at.work:25
> Is it possible to automatically shut down
> the forwarding when the client exits.
Coincidentally, I had a need to do this today, and my solution is
different again from either of the two so far posted. Here's mine.
First you'll need a named pipe (FIFO) file. I keep a permanent one
in $HOME/.f, because I often find it comes in handy. If you haven't
got one, make one:
mkfifo $HOME/.f # I keep this lying around all the time anyway
Now set up the SSH command and have it read input from this named
pipe. Simultaneously, open a spare file descriptor in your shell
which writes (no) output to the pipe.
ssh -L 5432:host.at.work:25 myhostname cat < $HOME/.f & exec 15>$HOME/.f
Now at the moment when your shell session terminates, there will no
longer be any processes writing to the named pipe; so the ssh
process will read end-of-file on its standard input and will
-- Simon Tatham "My heart bleeds. <email@example.com> (That's how it works.)" -- Gareth Taylor