private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) { try { LOG.trace("SSH session created on channel: {}", ctx.channel()); session = future.getSession(); final AuthFuture authenticateFuture = authenticationHandler.authenticate(session); authenticateFuture.addListener(new SshFutureListener<AuthFuture>() { @Override public void operationComplete(final AuthFuture future) { if (future.isSuccess()) { handleSshAuthenticated(session, ctx); } else { // Exception does not have to be set in the future, add simple exception in such case final Throwable exception = future.getException() == null ? new IllegalStateException("Authentication failed") : future.getException(); handleSshSetupFailure(ctx, exception); } } }); } catch (final IOException e) { handleSshSetupFailure(ctx, e); } }
private synchronized void handleSshSessionCreated(final ConnectFuture future, final ChannelHandlerContext ctx) { try { LOG.trace("SSH session created on channel: {}", ctx.channel()); session = future.getSession(); final AuthFuture authenticateFuture = authenticationHandler.authenticate(session); authenticateFuture.addListener(new SshFutureListener<AuthFuture>() { @Override public void operationComplete(final AuthFuture future) { if (future.isSuccess()) { handleSshAuthenticated(session, ctx); } else { // Exception does not have to be set in the future, add simple exception in such case final Throwable exception = future.getException() == null ? new IllegalStateException("Authentication failed") : future.getException(); handleSshSetupFailure(ctx, exception); } } }); } catch (final IOException e) { handleSshSetupFailure(ctx, e); } }
authFuture.addListener(new SshFutureListener<AuthFuture>()
private static ClientSession connect(SshClient client, final String user, KeyPair keyPair, final String ip, final int port) throws IOException, InterruptedException { ClientSession session = client.connect(user, ip, port).await().getSession(); int authState = ClientSession.WAIT_AUTH; while ((authState & ClientSession.WAIT_AUTH) != 0) { session.addPublicKeyIdentity(keyPair); log.info("Authenticating to " + user + "@" + ip); AuthFuture authFuture = session.auth(); authFuture.addListener(new SshFutureListener<AuthFuture>() { @Override public void operationComplete(AuthFuture authFuture) { log.info("Authentication completed with " + (authFuture.isSuccess() ? "success" : "failure") + " for " + user + "@" + ip + ":" + port); } }); authState = session.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0); } if ((authState & ClientSession.CLOSED) != 0) { throw new IOException("Authentication failed for " + user + "@" + ip); } return session; }