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.

2
  • Feedback on existing answer? Works? Doesn't works? Commented Jun 13, 2018 at 7:14
  • @TarunLalwani I am extremely busy, will test it when I have time.
    – Jan Wytze
    Commented Jun 13, 2018 at 11:23

1 Answer 1

2
+50

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();
2
  • 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
    Commented Jun 14, 2018 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
    Commented Jun 14, 2018 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.