I am using the following options in my ~/.ssh/config
in order to enable connection multiplexing:
ControlMaster auto
ControlPath ~/.ssh/control/master-%r@%h:%p
However, this has the annoying problem that the first shell to connect to a particular server must be the last to disconnect, because it is the master connection that all the other connections are using. So if you log out of the master, it appears to just hang. To solve this, I would like to wrap ssh with a script that checks if the control master file exists, and if not, starts a master ssh process in the background. Then it would start a slave ssh session.
In order to accomplish this, my script would have to determine the path to the control file that ssh would use. This would entail parsing the ssh command line options and config files and implementing the logic for determining the ControlPath. Is there any way to just ask ssh what path it would use, so I can check it?
Just found it.
ssh -O check HOST
From my tests the accepted answer:
will only give the control file if it doesn't exits
otherwise
However, grep and
ssh -G
will always give you the path.Note: If you use
%l
or%C
(for example) in the ssh config file this will evaluate for the local machine.