产生SSH连接,但检查状态?

I'm writing a program in Go that needs to call ssh as a subprocess, but then once the connection is set up, use it for other things. I'm trying to figure out how to check whether the connection has been set up properly. The issue is that I can't wait for the exit code, since the command doesn't exit until a successful connection, but if I don't wait, then it's possible that I'll fork the process and do other things but then the subprocess will encounter an error and die. What I need is a way to know that, after a certain point, the connection has been successfully established. For example, one option would be to have ssh establish the connection and then, after it's established, fork the process owning the connection and having the main process return with a success code. However, I can't figure out if ssh allows for this sort of behavior.

Figured it out. In particular, I'm using "ControlMaster" connection multiplexing, having one ssh process act as a master which keeps the actual ssh connection open, and allows other connections to multiplex streams onto the connection.

First, do:

ssh -o ControlMaster=yes -o ControlPath=/path/to/socket -o ControlPersist=yes \
    user@hostname true

The important part here is that ControlPersist=yes tells ssh to continue running in the background even after the main connection stream has closed. Executing true on the remote server causes it to connect, try executing a command, and return. Once this happens, if it was successful, ssh will return with a success exit code.

Then you can connect using non-master ssh processes:

ssh -o ControlMaster=no -o ControlPath=/path/to/socket user@hostname ...