Search in sources :

Example 1 with SshClient

use of org.apache.sshd.SshClient 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 SshClient

use of org.apache.sshd.SshClient in project fabric8 by jboss-fuse.

the class ContainerConnectAction method executSshCommand.

/**
 * Executes the ssh command.
 */
private void executSshCommand(CommandSession session, String username, String password, String hostname, String port, String cmd) throws Exception {
    if (cmd == null || cmd.length() == 0) {
        // ENTESB-6826: we're connecting in "shell" mode, which isn't wise when running from bin/client or ssh
        if (session.getKeyboard().getClass().getName().equals("org.apache.sshd.common.channel.ChannelPipedInputStream")) {
            System.err.println("When connecting to remote container using \"fabric:container-connect\" using ssh or bin/client, please establish SSH session (run bin/client) first and then run \"fabric:container-connect\"");
            return;
        }
    }
    // Create the client from prototype
    SshClient client = createClient();
    String agentSocket;
    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);
    }
    try {
        ConnectFuture future = client.connect(hostname, Integer.parseInt(port));
        future.await();
        sshSession = future.getSession();
        Object oldIgnoreInterrupts = this.session.get(Console.IGNORE_INTERRUPTS);
        this.session.put(Console.IGNORE_INTERRUPTS, Boolean.TRUE);
        try {
            System.out.println("Connected");
            boolean authed = false;
            if (!authed) {
                if (username == null) {
                    throw new FabricAuthenticationException("No username specified.");
                }
                log.debug("Prompting user for password");
                String pwd = password != null ? password : ShellUtils.readLine(session, "Password: ", true);
                sshSession.authPassword(username, pwd);
                int ret = sshSession.waitFor(ClientSession.WAIT_AUTH | ClientSession.CLOSED | ClientSession.AUTHED, 0);
                if ((ret & ClientSession.AUTHED) == 0) {
                    System.err.println("Password authentication failed");
                } else {
                    authed = true;
                }
            }
            if (!authed) {
                throw new FabricAuthenticationException("Failed to authenticate.");
            }
            // If user is authenticated credentials to session for future use.
            ShellUtils.storeFabricCredentials(session, username, password);
            ClientChannel channel;
            if (cmd != null && cmd.length() > 0) {
                channel = sshSession.createChannel("exec", cmd);
                channel.setIn(new ByteArrayInputStream(new byte[0]));
            } else {
                channel = sshSession.createChannel("shell");
                channel.setIn(new NoCloseInputStream(System.in));
                ((ChannelShell) channel).setPtyColumns(ShellUtils.getTermWidth(session));
                ((ChannelShell) channel).setupSensibleDefaultPty();
                ((ChannelShell) channel).setAgentForwarding(true);
            }
            channel.setOut(new NoCloseOutputStream(System.out));
            channel.setErr(new NoCloseOutputStream(System.err));
            channel.open();
            channel.waitFor(ClientChannel.CLOSED, 0);
        } finally {
            session.put(Console.IGNORE_INTERRUPTS, oldIgnoreInterrupts);
            sshSession.close(false);
        }
    } finally {
        client.stop();
    }
}
Also used : NoCloseInputStream(org.apache.sshd.common.util.NoCloseInputStream) SshClient(org.apache.sshd.SshClient) FabricAuthenticationException(io.fabric8.api.FabricAuthenticationException) ByteArrayInputStream(java.io.ByteArrayInputStream) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) ChannelShell(org.apache.sshd.client.channel.ChannelShell) NoCloseOutputStream(org.apache.sshd.common.util.NoCloseOutputStream) ClientChannel(org.apache.sshd.ClientChannel)

Example 3 with SshClient

use of org.apache.sshd.SshClient in project ovirt-engine by oVirt.

the class TimeoutTest method testPasswordTimeout.

@Test(expected = TimeLimitExceededException.class)
public void testPasswordTimeout() throws Exception {
    SSHClient client = spy(this.client);
    SshClient ssh = spy(SshClient.setUpDefaultClient());
    ConnectFuture future = mock(ConnectFuture.class);
    ClientSession session = mock(ClientSession.class);
    doReturn(ssh).when(client).createSshClient();
    doReturn(future).when(ssh).connect(any(), anyInt());
    when(future.await(anyLong())).thenReturn(true);
    when(future.getSession()).thenReturn(session);
    AuthFuture authFuture = mock(AuthFuture.class);
    when(authFuture.await(anyLong())).thenReturn(false);
    when(session.authPassword(any(), any())).thenReturn(authFuture);
    client.connect();
    client.authenticate();
}
Also used : SshClient(org.apache.sshd.SshClient) ClientSession(org.apache.sshd.ClientSession) ConnectFuture(org.apache.sshd.client.future.ConnectFuture) AuthFuture(org.apache.sshd.client.future.AuthFuture) Test(org.junit.Test)

Example 4 with SshClient

use of org.apache.sshd.SshClient in project alien4cloud by alien4cloud.

the class SSHUtil method doWithScp.

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);
    }
}
Also used : SshClient(org.apache.sshd.SshClient) ClientSession(org.apache.sshd.ClientSession) ScpClient(org.apache.sshd.client.ScpClient)

Example 5 with SshClient

use of org.apache.sshd.SshClient 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)

Aggregations

SshClient (org.apache.sshd.SshClient)7 ConnectFuture (org.apache.sshd.client.future.ConnectFuture)4 ClientSession (org.apache.sshd.ClientSession)3 Test (org.junit.Test)2 VaadinRequest (com.vaadin.server.VaadinRequest)1 UI (com.vaadin.ui.UI)1 VerticalLayout (com.vaadin.ui.VerticalLayout)1 FabricAuthenticationException (io.fabric8.api.FabricAuthenticationException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ClientChannel (org.apache.sshd.ClientChannel)1 ScpClient (org.apache.sshd.client.ScpClient)1 ChannelShell (org.apache.sshd.client.channel.ChannelShell)1 AuthFuture (org.apache.sshd.client.future.AuthFuture)1 NoCloseInputStream (org.apache.sshd.common.util.NoCloseInputStream)1 NoCloseOutputStream (org.apache.sshd.common.util.NoCloseOutputStream)1 Before (org.junit.Before)1 SshException (org.platformlayer.ops.ssh.SshException)1