3

I'm using the following line in my scripts:

ssh -f -N -M -S <control socket> <host>

This means the initial connection just stays in the background and I can use it for subsequent calls to ssh:

ssh -S <control socket> <host> <command>

However, if I have multiple scripts with commands which are supposed to use the same control socket and put the "background" call to ssh into all of them, I will get the following message at some point:

ControlSocket <control socket> already exists, disabling multiplexing

This has no influence over the rest of the script because obviously the socket exists and can be used by the subsequent ssh commands. However, even though the "background" session couldn't open the socket, it doesn't quit and stays active - just without multiplexing.

Using [ -S or ssh -O check to check the existence of the socket would still leave the possibility of race conditions.

How can I do "open control socket if it doesn't exist yet, and exit if it does"?

5

I think you are looking for ControlMaster auto, which can be either specified in configuration file or directly on command-line with -o ControlMaster=auto.

This allows you to unify the commands opening the connection and using it (also very helpful with ControlPersist).

  • Unfortunately, the -M sets it to master, which is not what you want for transparent usage of the multiplexing. – Jakuje Apr 18 '20 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.