apache / mina-sshd

Apache MINA sshd is a comprehensive Java library for client- and server-side SSH.
https://mina.apache.org/sshd-project/
Apache License 2.0
847 stars 353 forks source link

mina-sshd How does sshClient use the ssh ControlMaster feature #513

Closed czldb2 closed 3 weeks ago

czldb2 commented 4 weeks ago

image My program uses sshClient to create a ChannelExec to do things like :"ssh user@ip cmd". I now want to reuse this connection so that I don't have to re-establish the ssh connection the next time I execute "ssh user@ip cmd". How do I achieve this?

tomaswolf commented 4 weeks ago

ControlMaster is not implemented, and neither are ControlPath nor ControlPersist.

Depending on your use case, you can just create a session, and then create multiple channels, as you need. Sequentially, or even in parallel. (Unless the server restricts the number of channels somehow, but then you'd also have a problem with ControlMaster connections.) Just keep the session around for as long as you need it. Possibly use heartbeats to prevent the peer from closing the session prematurely.

If your use case involves multiple processes, you'd have to implement some mechanism akin to ControlMaster and ControlPath yourself. But note that the default NIO2 transport in Apache MINA SSHD uses AsynchronousSocketChannels, which do not support using pre-existing sockets. Perhaps it might be possible to build something using the MINA or Netty transports, or by customizing those.

(The control master would handle the SSH transport protocol including key exchanges and encryption/decryption, plus authentication, while all the clients of the master would only do the SSH connection (i.e., channel) protocol. The connections between the master and its clients are unencrypted, and thus had better be local. The two protocol layers (transport and connection) are not well separated in Apache MINA SSHD, so this might involve some serious refactoring.)

czldb2 commented 3 weeks ago

ControlMaster is not implemented, and neither are ControlPath nor ControlPersist.

Depending on your use case, you can just create a session, and then create multiple channels, as you need. Sequentially, or even in parallel. (Unless the server restricts the number of channels somehow, but then you'd also have a problem with ControlMaster connections.) Just keep the session around for as long as you need it. Possibly use heartbeats to prevent the peer from closing the session prematurely.

If your use case involves multiple processes, you'd have to implement some mechanism akin to ControlMaster and ControlPath yourself. But note that the default NIO2 transport in Apache MINA SSHD uses AsynchronousSocketChannels, which do not support using pre-existing sockets. Perhaps it might be possible to build something using the MINA or Netty transports, or by customizing those.

(The control master would handle the SSH transport protocol including key exchanges and encryption/decryption, plus authentication, while all the clients of the master would only do the SSH connection (i.e., channel) protocol. The connections between the master and its clients are unencrypted, and thus had better be local. The two protocol layers (transport and connection) are not well separated in Apache MINA SSHD, so this might involve some serious refactoring.)

Thank you very much for your reply, which is very helpful to me.