Apache SSHD 客户端获取服务器 public 密钥

Apache SSHD client get server public key

我正在尝试获取服务器的 public 密钥。这是我试过的:

val serverKey = sshClient.connect("dyn mem", "localhost", "2222")
  .verify()
  .getSession()
  .getKex()
  .getServerKey()

问题是 getServerKey() 的结果为空...

如何使用 Apache SSHD 客户端获取 SSH 服务器的 public 密钥。

connect()和后续的密钥交换都是异步操作,所以需要等待几次。例如。 :

        ConnectFuture connectFuture = client.connect(username, host, 22);
        connectFuture.await(5000);

        ClientSession session = connectFuture.getSession();
        session.waitFor(Arrays.asList(ClientSessionEvent.WAIT_AUTH), 5000);

        session.getKex().getServerKey();