Skip to content

Instantly share code, notes, and snippets.

@devster31
Created August 16, 2017 12:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devster31/c528372fa5c3e94659d2fb810c331e6f to your computer and use it in GitHub Desktop.
Save devster31/c528372fa5c3e94659d2fb810c331e6f to your computer and use it in GitHub Desktop.
Bash script to setup 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 -fnNT -L 50000:localhost:3306 jm@sampledomain.com
$ ssh -S my-ctrl-socket -O check jm@sampledomain.com
Master running (pid=3517) 
$ ssh -S my-ctrl-socket -O exit jm@sampledomain.com
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment