SshClient client = SshClient.setUpDefaultClient(); client.start(); ClientSession session = client.connect("localhost", port).await().getSession(); session.authPassword("root", "");
public SSHClient close() { try { Utils.close(out); channel.close(false); session.close(false); client.stop(); return this; } finally { this.client = null; this.channel = null; this.session = null; this.out = null; } } }
protected void setupAgent(String user, File keyFile, SshClient client) { final URL builtInPrivateKey = KarafClient.class.getClassLoader().getResource("karaf.key"); final SshAgent agent = startAgent(user, builtInPrivateKey, keyFile); client.setAgentFactory(new LocalAgentFactory(agent)); client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local"); }
protected SshClient createClient() { // must create a new client for each usage SshClient client = SshClient.setUpDefaultClient(); client.setAgentFactory(agentFactory); // must start client client.start(); log.trace("Created SshClient: {}", client); return client; }
Terminal terminal = null; try { client = SshClient.setUpDefaultClient(); client.start(); int retries = 0; ClientSession session = null; do { ConnectFuture future = client.connect(host, port); future.await(); try { } finally { try { client.stop(); } catch (Throwable t) { } try {
if (this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME) != null) { agentSocket = this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME).toString(); client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME,agentSocket); ConnectFuture future = client.connect(hostname, Integer.parseInt(port)); future.await(); sshSession = future.getSession(); client.stop();
@Override protected void connectInternal() throws Exception { log.info("Connecting to server {}:{}", configuration.getHost(), configuration.getPort()); client = SshClient.setUpDefaultClient(); setupAgent(configuration.getUsername(), configuration.getKeyFile(), client); client.start(); connect(client); if (configuration.getPassword() != null) { session.addPasswordIdentity(configuration.getPassword()); } session.auth().verify(); }
private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) { LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel()); final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address); sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() { @Override public void operationComplete(final ConnectFuture future) { if (future.isConnected()) { handleSshSessionCreated(future, ctx); } else { handleSshSetupFailure(ctx, future.getException()); } } }); }
private static void doWithScp(String user, String ip, int port, String pemPath, DoWithScpAction doWithScpAction) throws IOException, InterruptedException { SshClient client = SshClient.setUpDefaultClient(); ClientSession session = null; try { client.start(); session = connect(client, user, loadKeyPair(pemPath), ip, port); ScpClient scpClient = session.createScpClient(); doWithScpAction.doScpAction(scpClient); } finally { if (session != null) { session.close(true); } client.close(true); } }
/** * * @param authenticationHandler * @param sshClient started SshClient * @throws IOException */ public AsyncSshHandler(final AuthenticationHandler authenticationHandler, final SshClient sshClient) throws IOException { this.authenticationHandler = Preconditions.checkNotNull(authenticationHandler); this.sshClient = Preconditions.checkNotNull(sshClient); // Start just in case sshClient.start(); }
client = SshClient.setUpDefaultClient(); client.start(); ConnectFuture future = client.connect(host, Integer.parseInt(port)); ClientSession session = future.await().getSession(); if (!session.authPassword(user, pass).await().isSuccess()) { } finally { try { client.stop(); } catch (Throwable t) { }
if (this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME) != null) { agentSocket = this.session.get(SshAgent.SSH_AUTHSOCKET_ENV_NAME).toString(); client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME,agentSocket); ConnectFuture future = client.connect(hostname, Integer.parseInt(port)); future.await(); sshSession = future.getSession(); client.stop();
client = SshClient.setUpDefaultClient(); client.start(); loginButton.addClickListener(this); GridLayout grid = new GridLayout(2,2);
private void startSsh(final ChannelHandlerContext ctx, final SocketAddress address) { LOG.debug("Starting SSH to {} on channel: {}", address, ctx.channel()); final ConnectFuture sshConnectionFuture = sshClient.connect(authenticationHandler.getUsername(), address); sshConnectionFuture.addListener(new SshFutureListener<ConnectFuture>() { @Override public void operationComplete(final ConnectFuture future) { if (future.isConnected()) { handleSshSessionCreated(future, ctx); } else { handleSshSetupFailure(ctx, future.getException()); } } }); }
private SshClient getSshClient(BlueprintContainer blueprintContainer, String clientId) { // Create the client from prototype SshClient client = (SshClient) blueprintContainer.getComponentInstance(clientId); log.debug("Created client: {}", client); client.start(); return client; }
public void connect() { sshClient = SshClient.setUpDefaultClient(); sshClient.start(); try { clientSession = sshClient.connect(EncryptionTool.getInstance().decrypt(username), host, port).await().getSession(); if (password != null) { clientSession.addPasswordIdentity(EncryptionTool.getInstance().decrypt(password));
protected void connect(SshClient client) throws IOException, InterruptedException { int attempts = 10; do { final ConnectFuture future = client.connect(configuration.getUsername(), configuration.getHost(), configuration.getPort()); future.await(); try { session = future.getSession(); } catch (RuntimeSshException ex) { if (--attempts > 0) { Thread.sleep(TimeUnit.SECONDS.toMillis(2)); log.info("Waiting for SSH connection..."); } else { throw ex; } } } while (session == null); } }
@Override protected void closeInternal() throws IOException { session.close(true); session = null; client.stop(); client = null; }
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; }
AsyncSshHandler.DEFAULT_CLIENT.stop();