Bash script to set up a temporary SSH tunnel

You can do this cleanly with an ssh ‘control socket’. To talk to an already-running SSH process and get it’s pid, kill it etc. Use the ‘control socket’ (-M for master and -S for socket) as follows:

$ ssh -M -S my-ctrl-socket -fNT -L 50000:localhost:3306
$ ssh -S my-ctrl-socket -O check
Master running (pid=3517) 
$ ssh -S my-ctrl-socket -O exit
Exit request sent. 

Note that my-ctrl-socket will be an actual file that is created.

I got this info from a very RTFM reply on the OpenSSH mailing list.

Leave a Comment