Jan Wytze
Jan Wytze

Reputation: 3497

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.

Upvotes: 5

Views: 1778

Answers (1)

df778899
df778899

Reputation: 10931

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

Upvotes: 2

Related Questions