5

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.

Jan Wytze
  • 3,307
  • 5
  • 31
  • 51

1 Answers1

2

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();
df778899
  • 10,703
  • 1
  • 24
  • 36
  • It did not solve the problem but I am sure it has something to do with it. When I debug and set the breakpoint after the connection it works. – Jan Wytze Jun 14 '18 at 20:43
  • It looks like the key exchange after the session has connected is async too, so there is a further wait for this. This is also in the answer now. – df778899 Jun 14 '18 at 21:48