Skip to content

Instantly share code, notes, and snippets.

@aculich
Last active November 30, 2020 15:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aculich/4265549 to your computer and use it in GitHub Desktop.
Save aculich/4265549 to your computer and use it in GitHub Desktop.
This is a side-by-side session showing the `ssh -O ctl_cmd` in action; with port `forward`/`cancel` from localhost on the left side, and the output of ssh connected to remotehost on the right side. You may want to view this in raw mode to see it best. See: https://serverfault.com/questions/457295/removing-port-forwardings-programmatically-on-a-c…
localhost$ ssh -v -M -S/tmp/controlmaster-remotehost remotehost
OpenSSH_6.0p1 Debian-3ubuntu1, OpenSSL 1.0.1c 10 May 2012
...
Authenticated to remotehost.lxc ([10.0.3.167]:22).
debug1: setting up multiplex master socket
debug1: channel 0: new [/tmp/controlmaster-remotehost]
debug1: channel 1: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
Welcome to Ubuntu 12.10 (GNU/Linux 3.5.0-19-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Wed Dec 12 05:41:55 2012 from 10.0.3.1
localhost$ ssh -O check -S/tmp/controlmaster-remotehost remotehost root@remotehost:~# debug1: multiplexing control connection
Master running (pid=15637) debug1: channel 2: new [mux-control]
debug1: channel 2: free: mux-control, nchannels 3
localhost$ ssh -O forward -S/tmp/controlmaster-remotehost -L5555:localhost:22 remotehost debug1: multiplexing control connection
debug1: channel 2: new [mux-control]
debug1: Local forwarding listening on ::1 port 5555.
debug1: channel 3: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 5555.
debug1: channel 4: new [port listener]
debug1: channel 2: free: mux-control, nchannels 5
localhost$ echo bogus | nc localhost 5555 || echo not listening debug1: Connection to port 5555 forwarding to localhost port 22 requested.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1 debug1: channel 2: new [direct-tcpip]
Protocol mismatch. debug1: channel 2: free: direct-tcpip: listening port 5555 for localhost port 22, connect from 127.0.0.1 port 38424, nchannels 5
localhost$ ssh -O forward -S/tmp/controlmaster-remotehost -L3333:localhost:22 remotehost debug1: multiplexing control connection
debug1: channel 2: new [mux-control]
debug1: Local forwarding listening on ::1 port 3333.
debug1: channel 5: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 3333.
debug1: channel 6: new [port listener]
debug1: channel 2: free: mux-control, nchannels 7
localhost$ echo bogus | nc localhost 3333 || echo not listening debug1: Connection to port 3333 forwarding to localhost port 22 requested.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1 debug1: channel 2: new [direct-tcpip]
Protocol mismatch. debug1: channel 2: free: direct-tcpip: listening port 3333 for localhost port 22, connect from 127.0.0.1 port 60865, nchannels 7
localhost$ ssh -O cancel -S/tmp/controlmaster-remotehost -L5555:localhost:22 remotehost debug1: multiplexing control connection
debug1: channel 2: new [mux-control]
debug1: channel 3: free: port listener, nchannels 7
debug1: channel 4: free: port listener, nchannels 6
debug1: channel 2: free: mux-control, nchannels 5
localhost$ echo bogus | nc localhost 3333 || echo not listening debug1: Connection to port 3333 forwarding to localhost port 22 requested.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1 debug1: channel 2: new [direct-tcpip]
Protocol mismatch. debug1: channel 2: free: direct-tcpip: listening port 3333 for localhost port 22, connect from 127.0.0.1 port 60867, nchannels 5
localhost$ echo bogus | nc localhost 5555 || echo not listening
not listening
localhost$ ssh -O forward -S/tmp/controlmaster-remotehost -L5555:localhost:22 remotehost debug1: multiplexing control connection
debug1: channel 2: new [mux-control]
debug1: Local forwarding listening on ::1 port 5555.
debug1: channel 3: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 5555.
debug1: channel 4: new [port listener]
debug1: channel 2: free: mux-control, nchannels 7
localhost$ echo bogus | nc localhost 5555 || echo not listening debug1: Connection to port 5555 forwarding to localhost port 22 requested.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1 debug1: channel 2: new [direct-tcpip]
Protocol mismatch. debug1: channel 2: free: direct-tcpip: listening port 5555 for localhost port 22, connect from 127.0.0.1 port 38431, nchannels 7
localhost$ ssh -O cancel -S/tmp/controlmaster-remotehost -L5555:localhost:22 -L3333:localhost:22 remotehost debug1: multiplexing control connection
localhost$ echo bogus | nc localhost 3333 || echo not listening debug1: channel 2: new [mux-control]
not listening debug1: channel 3: free: port listener, nchannels 7
localhost$ echo bogus | nc localhost 5555 || echo not listening debug1: channel 4: free: port listener, nchannels 6
not listening debug1: channel 5: free: port listener, nchannels 5
debug1: channel 6: free: port listener, nchannels 4
debug1: channel 2: free: mux-control, nchannels 3
localhost$ ssh -O forward -S/tmp/controlmaster-remotehost -L5555:localhost:22 -L3333:localhost:22 remotehost debug1: multiplexing control connection
debug1: channel 2: new [mux-control]
debug1: Local forwarding listening on ::1 port 5555.
debug1: channel 3: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 5555.
debug1: channel 4: new [port listener]
debug1: Local forwarding listening on ::1 port 3333.
debug1: channel 5: new [port listener]
debug1: Local forwarding listening on 127.0.0.1 port 3333.
debug1: channel 6: new [port listener]
debug1: channel 2: free: mux-control, nchannels 7
localhost$ echo bogus | nc localhost 5555 || echo not listening debug1: Connection to port 5555 forwarding to localhost port 22 requested.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1 debug1: channel 2: new [direct-tcpip]
Protocol mismatch. debug1: channel 2: free: direct-tcpip: listening port 5555 for localhost port 22, connect from 127.0.0.1 port 38435, nchannels 7
localhost$ echo bogus | nc localhost 3333 || echo not listening debug1: Connection to port 3333 forwarding to localhost port 22 requested.
SSH-2.0-OpenSSH_6.0p1 Debian-3ubuntu1 debug1: channel 2: new [direct-tcpip]
Protocol mismatch. debug1: channel 2: free: direct-tcpip: listening port 3333 for localhost port 22, connect from 127.0.0.1 port 60876, nchannels 7
localhost$ ssh -O exit -S/tmp/controlmaster-remotehost remotehost debug1: multiplexing control connection
debug1: channel 2: new [mux-control]
debug1: channel 0: free: /tmp/controlmaster-remotehost, nchannels 7
debug1: channel 1: free: client-session, nchannels 6
debug1: channel 2: free: mux-control, nchannels 5
debug1: channel 3: free: port listener, nchannels 4
debug1: channel 4: free: port listener, nchannels 3
debug1: channel 5: free: port listener, nchannels 2
debug1: channel 6: free: port listener, nchannels 1
Connection to remotehost.lxc closed.
Transferred: sent 3784, received 3216 bytes, in 394.4 seconds
Bytes per second: sent 9.6, received 8.2
Exit request sent. debug1: Exit status -1
localhost$ localhost$
@tzot
Copy link

tzot commented Nov 4, 2020

This in your .ssh/config would really shorten the typing:

Host remotehost
  ControlMaster auto
  ControlPath /tmp/controlmaster-remotehost

All of the -S/tmp/controlmaster-remotehost arguments can be skipped.

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