Search in sources :

Example 1 with ConnectFuture

use of org.apache.sshd.client.future.ConnectFuture in project platformlayer by platformlayer.

the class MinaSshConnectionWrapper method connect.

public void connect(TimeSpan connectTimeout) throws SshException {
    if (state != SshConnectionState.NotConnected) {
        throw new IllegalStateException();
    }
    try {
        SshClient client = sshContext.client;
        System.out.println("New SSH connection to " + connectionInfo.getHost());
        ConnectFuture connect = client.connect(connectionInfo.getSocketAddress());
        if (!connect.await(connectTimeout.getTotalMilliseconds())) {
            connect.cancel();
            throw new SshException("Timeout while waiting for SSH connection to " + connectionInfo.getHost());
        }
        this.sshClientSession = connect.getSession();
        if (this.sshClientSession == null) {
            throw new IllegalStateException();
        }
        this.state = SshConnectionState.Connected;
    } catch (Exception e) {
        ExceptionUtils.handleInterrupted(e);
        throw new SshException("Error connecting to SSH server @" + connectionInfo.getSocketAddress(), e);
    }
}
Also used : SshClient(org.apache.sshd.SshClient) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) SshException(org.platformlayer.ops.ssh.SshException) IOException(java.io.IOException) SshException(org.platformlayer.ops.ssh.SshException)

Example 2 with ConnectFuture

use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.

the class SshKeyFormatTest method usePemKey.

@Test
public void usePemKey() throws Exception {
    SshClient client = SshClient.setUpDefaultClient();
    URL testPemURL = Resources.getResource(SshKeyFormatTest.class, "test.pem");
    ByteSource source = Resources.asByteSource(testPemURL);
    PKCS8Key pkcs8 = new PKCS8Key(source.openStream(), null);
    String sshPort = getSshPort();
    client.setServerKeyVerifier(new RequiredServerKeyVerifier(pkcs8.getPublicKey()));
    client.start();
    ConnectFuture future = client.connect("karaf", "localhost", Integer.parseInt(sshPort));
    future.await();
    ClientSession session = future.getSession();
    Set<ClientSessionEvent> ret = EnumSet.of(ClientSessionEvent.WAIT_AUTH);
    while (ret.contains(ClientSessionEvent.WAIT_AUTH)) {
        session.addPasswordIdentity("karaf");
        session.auth().verify();
        ret = session.waitFor(EnumSet.of(ClientSessionEvent.WAIT_AUTH, ClientSessionEvent.CLOSED, ClientSessionEvent.AUTHED), 0);
    }
    if (ret.contains(ClientSessionEvent.CLOSED)) {
        throw new Exception("Could not open SSH channel");
    }
    session.close(true);
}
Also used : PKCS8Key(org.apache.commons.ssl.PKCS8Key) ClientSessionEvent(org.apache.sshd.client.session.ClientSession.ClientSessionEvent) SshClient(org.apache.sshd.client.SshClient) RequiredServerKeyVerifier(org.apache.sshd.client.keyverifier.RequiredServerKeyVerifier) ClientSession(org.apache.sshd.client.session.ClientSession) ByteSource(com.google.common.io.ByteSource) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) URL(java.net.URL) Test(org.junit.Test)

Example 3 with ConnectFuture

use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.

the class Main method connectWithRetries.

private static ClientSession connectWithRetries(SshClient client, ClientConfig config) throws Exception {
    ClientSession session = null;
    int retries = 0;
    do {
        ConnectFuture future = client.connect(config.getUser(), config.getHost(), config.getPort());
        future.await();
        try {
            session = future.getSession();
        } catch (RuntimeSshException ex) {
            if (retries++ < config.getRetryAttempts()) {
                Thread.sleep(config.getRetryDelay() * 1000);
                System.out.println("retrying (attempt " + retries + ") ...");
            } else {
                throw ex;
            }
        }
    } while (session == null);
    return session;
}
Also used : ClientSession(org.apache.sshd.client.session.ClientSession) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 4 with ConnectFuture

use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.

the class DeployMojo method connect.

private ClientSession connect(SshClient client) throws IOException, InterruptedException {
    int retries = 0;
    ClientSession session = null;
    do {
        final ConnectFuture future = client.connect(user, host, port);
        future.await();
        try {
            session = future.getSession();
        } catch (RuntimeSshException ex) {
            if (retries++ < attempts) {
                Thread.sleep(TimeUnit.SECONDS.toMillis(delay));
                getLog().info("retrying (attempt " + retries + ") ...");
            } else {
                throw ex;
            }
        }
    } while (session == null);
    return session;
}
Also used : ClientSession(org.apache.sshd.client.session.ClientSession) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) RuntimeSshException(org.apache.sshd.common.RuntimeSshException)

Example 5 with ConnectFuture

use of org.apache.sshd.client.future.ConnectFuture in project karaf by apache.

the class SshAction method connectWithRetries.

private static ClientSession connectWithRetries(SshClient client, String username, String host, int port, int maxAttempts) throws Exception {
    ClientSession session = null;
    int retries = 0;
    do {
        ConnectFuture future = client.connect(username, host, port);
        future.await();
        try {
            session = future.getSession();
        } catch (Exception ex) {
            if (retries++ < maxAttempts) {
                Thread.sleep(2 * 1000);
                System.out.println("retrying (attempt " + retries + ") ...");
            } else {
                throw ex;
            }
        }
    } while (session == null);
    return session;
}
Also used : ClientSession(org.apache.sshd.client.session.ClientSession) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) IOException(java.io.IOException)

Aggregations

ConnectFuture (org.apache.sshd.client.future.ConnectFuture)9 ClientSession (org.apache.sshd.client.session.ClientSession)6 RuntimeSshException (org.apache.sshd.common.RuntimeSshException)4 IOException (java.io.IOException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ClientSessionEvent (org.apache.sshd.client.session.ClientSession.ClientSessionEvent)2 ByteSource (com.google.common.io.ByteSource)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 URL (java.net.URL)1 KeyPair (java.security.KeyPair)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 PKCS8Key (org.apache.commons.ssl.PKCS8Key)1 ClientChannel (org.apache.sshd.ClientChannel)1 ClientSession (org.apache.sshd.ClientSession)1 SshClient (org.apache.sshd.SshClient)1 SshClient (org.apache.sshd.client.SshClient)1 AuthFuture (org.apache.sshd.client.future.AuthFuture)1