Search code examples
javasshopensshsshdapache-mina

Apache SSHD client get server public key


I am trying to get the public key of a server. This is what I tried:

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

The problem is get the result of getServerKey() is null...

How can I get the public key of a SSH server with the Apache SSHD client.


Solution

  • Both connect(), and the subsequent key exchange are async operations, so a couple of waits are needed. E.g. :

            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();