Search in sources :

Example 1 with ClientSessionEvent

use of org.apache.sshd.client.session.ClientSession.ClientSessionEvent 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 2 with ClientSessionEvent

use of org.apache.sshd.client.session.ClientSession.ClientSessionEvent in project karaf by apache.

the class SshCommandTestBase method openSshChannel.

private OutputStream openSshChannel(String username, String password, OutputStream... outputs) throws Exception {
    client = SshClient.setUpDefaultClient();
    client.start();
    String sshPort = getSshPort();
    Awaitility.await().ignoreExceptions().until(() -> {
        ConnectFuture future = client.connect(username, "localhost", Integer.parseInt(sshPort));
        future.await();
        session = future.getSession();
        Set<ClientSessionEvent> ret = EnumSet.of(ClientSessionEvent.WAIT_AUTH);
        while (ret.contains(ClientSessionEvent.WAIT_AUTH)) {
            session.addPasswordIdentity(password);
            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");
        }
        return true;
    });
    channel = session.createChannel("shell");
    PipedOutputStream pipe = new PipedOutputStream();
    channel.setIn(new PipedInputStream(pipe));
    OutputStream out;
    if (outputs.length >= 1) {
        out = outputs[0];
    } else {
        out = new ByteArrayOutputStream();
    }
    channel.setOut(out);
    OutputStream err;
    if (outputs.length >= 2) {
        err = outputs[1];
    } else {
        err = new ByteArrayOutputStream();
    }
    channel.setErr(err);
    channel.open();
    return pipe;
}
Also used : ClientSessionEvent(org.apache.sshd.client.session.ClientSession.ClientSessionEvent) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedOutputStream(java.io.PipedOutputStream) PipedOutputStream(java.io.PipedOutputStream) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Aggregations

ConnectFuture (org.apache.sshd.client.future.ConnectFuture)2 ClientSessionEvent (org.apache.sshd.client.session.ClientSession.ClientSessionEvent)2 ByteSource (com.google.common.io.ByteSource)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 URL (java.net.URL)1 PKCS8Key (org.apache.commons.ssl.PKCS8Key)1 SshClient (org.apache.sshd.client.SshClient)1 RequiredServerKeyVerifier (org.apache.sshd.client.keyverifier.RequiredServerKeyVerifier)1 ClientSession (org.apache.sshd.client.session.ClientSession)1 Test (org.junit.Test)1