Search in sources :

Example 1 with ClientSession

use of org.apache.sshd.ClientSession in project opennms by OpenNMS.

the class SSHTerminalTest method setUp.

@SuppressWarnings("serial")
@Before
public void setUp() throws Exception {
    app = new UI() {

        @Override
        public void init(VaadinRequest request) {
        }
    };
    mainWindow = new VerticalLayout();
    app.setContent(mainWindow);
    SSHWindow sshWindow = new SSHWindow(null, 200, 200);
    app.addWindow(sshWindow);
    SshClient client = SshClient.setUpDefaultClient();
    client.start();
    ClientSession session = null;
    try {
        session = client.connect(testHost, testPort).await().getSession();
    } catch (Exception e) {
        fail("Could not connect to host");
    }
    sshTerm = new SSHTerminal(sshWindow, session, 200, 200);
    sshWindow.setContent(sshTerm);
    UI.setCurrent(app);
}
Also used : UI(com.vaadin.ui.UI) SshClient(org.apache.sshd.SshClient) ClientSession(org.apache.sshd.ClientSession) VerticalLayout(com.vaadin.ui.VerticalLayout) VaadinRequest(com.vaadin.server.VaadinRequest) Before(org.junit.Before)

Example 2 with ClientSession

use of org.apache.sshd.ClientSession in project tomee by apache.

the class SSHServerTest method call.

@Test(timeout = 10000L)
public void call() throws Exception {
    final SshClient client = SshClient.setUpDefaultClient();
    client.start();
    try {
        final ClientSession session = client.connect("localhost", 4222).await().getSession();
        session.authPassword("jonathan", "secret");
        final ClientChannel channel = session.createChannel("shell");
        ByteArrayOutputStream sent = new ByteArrayOutputStream();
        PipedOutputStream pipedIn = new TeePipedOutputStream(sent);
        channel.setIn(new PipedInputStream(pipedIn));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        channel.setOut(out);
        channel.setErr(err);
        channel.open();
        pipedIn.write("properties\r\n".getBytes());
        pipedIn.flush();
        pipedIn.write("exit\r\n".getBytes());
        pipedIn.flush();
        channel.waitFor(ClientChannel.CLOSED, 0);
        channel.close(false);
        client.stop();
        assertTrue(new String(sent.toByteArray()).contains("properties\r\nexit\r\n"));
        assertTrue(new String(out.toByteArray()).contains("ServerService(id=ssh)"));
    } catch (Exception e) {
        e.printStackTrace();
        fail();
    }
}
Also used : SshClient(org.apache.sshd.SshClient) ClientSession(org.apache.sshd.ClientSession) PipedOutputStream(java.io.PipedOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) ClientChannel(org.apache.sshd.ClientChannel) Test(org.junit.Test)

Example 3 with ClientSession

use of org.apache.sshd.ClientSession in project camel by apache.

the class SshHelper method sendExecCommand.

public static SshResult sendExecCommand(String command, SshEndpoint endpoint, SshClient client) throws Exception {
    SshResult result = null;
    SshConfiguration configuration = endpoint.getConfiguration();
    if (configuration == null) {
        throw new IllegalStateException("Configuration must be set");
    }
    ConnectFuture connectFuture = client.connect(null, configuration.getHost(), configuration.getPort());
    // Wait getTimeout milliseconds for connect operation to complete
    connectFuture.await(configuration.getTimeout());
    if (!connectFuture.isDone() || !connectFuture.isConnected()) {
        final String msg = "Failed to connect to " + configuration.getHost() + ":" + configuration.getPort() + " within timeout " + configuration.getTimeout() + "ms";
        LOG.debug(msg);
        throw new RuntimeCamelException(msg);
    }
    LOG.debug("Connected to {}:{}", configuration.getHost(), configuration.getPort());
    ClientChannel channel = null;
    ClientSession session = null;
    try {
        AuthFuture authResult;
        session = connectFuture.getSession();
        KeyPairProvider keyPairProvider;
        final String certResource = configuration.getCertResource();
        if (certResource != null) {
            LOG.debug("Attempting to authenticate using ResourceKey '{}'...", certResource);
            keyPairProvider = new ResourceHelperKeyPairProvider(new String[] { certResource }, endpoint.getCamelContext().getClassResolver());
        } else {
            keyPairProvider = configuration.getKeyPairProvider();
        }
        if (keyPairProvider != null) {
            LOG.debug("Attempting to authenticate username '{}' using Key...", configuration.getUsername());
            KeyPair pair = keyPairProvider.loadKey(configuration.getKeyType());
            authResult = session.authPublicKey(configuration.getUsername(), pair);
        } else {
            LOG.debug("Attempting to authenticate username '{}' using Password...", configuration.getUsername());
            authResult = session.authPassword(configuration.getUsername(), configuration.getPassword());
        }
        authResult.await(configuration.getTimeout());
        if (!authResult.isDone() || authResult.isFailure()) {
            LOG.debug("Failed to authenticate");
            throw new RuntimeCamelException("Failed to authenticate username " + configuration.getUsername());
        }
        channel = session.createChannel(ClientChannel.CHANNEL_EXEC, command);
        ByteArrayInputStream in = new ByteArrayInputStream(new byte[] { 0 });
        channel.setIn(in);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        channel.setOut(out);
        ByteArrayOutputStream err = new ByteArrayOutputStream();
        channel.setErr(err);
        OpenFuture openFuture = channel.open();
        openFuture.await(configuration.getTimeout());
        if (openFuture.isOpened()) {
            channel.waitFor(ClientChannel.CLOSED, 0);
            result = new SshResult(command, channel.getExitStatus(), new ByteArrayInputStream(out.toByteArray()), new ByteArrayInputStream(err.toByteArray()));
        }
        return result;
    } finally {
        if (channel != null) {
            channel.close(true);
        }
        // need to make sure the session is closed 
        if (session != null) {
            session.close(false);
        }
    }
}
Also used : KeyPair(java.security.KeyPair) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ClientChannel(org.apache.sshd.ClientChannel) KeyPairProvider(org.apache.sshd.common.KeyPairProvider) OpenFuture(org.apache.sshd.client.future.OpenFuture) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientSession(org.apache.sshd.ClientSession) RuntimeCamelException(org.apache.camel.RuntimeCamelException) AuthFuture(org.apache.sshd.client.future.AuthFuture)

Aggregations

ClientSession (org.apache.sshd.ClientSession)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ClientChannel (org.apache.sshd.ClientChannel)2 SshClient (org.apache.sshd.SshClient)2 VaadinRequest (com.vaadin.server.VaadinRequest)1 UI (com.vaadin.ui.UI)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 KeyPair (java.security.KeyPair)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 AuthFuture (org.apache.sshd.client.future.AuthFuture)1 ConnectFuture (org.apache.sshd.client.future.ConnectFuture)1 OpenFuture (org.apache.sshd.client.future.OpenFuture)1 KeyPairProvider (org.apache.sshd.common.KeyPairProvider)1 Before (org.junit.Before)1 Test (org.junit.Test)1