Package org.apache.sshd

Examples of org.apache.sshd.ClientSession


        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 {
                    session = future.getSession();
                } catch (RuntimeSshException ex) {
                    if (retries++ < retryAttempts) {
                        Thread.sleep(retryDelay * 1000);
                        System.out.println("retrying (attempt " + retries + ") ...");
                    } else {
                        throw ex;
                    }
                }
            } while (session == null);
            if (!session.authPassword(user, password).await().isSuccess()) {
                throw new Exception("Authentication failure");
            }
            ClientChannel channel;
      if (sb.length() > 0) {
                channel = session.createChannel("exec", sb.append("\n").toString());
                channel.setIn(new ByteArrayInputStream(new byte[0]));
      } else {
                terminal = new TerminalFactory().getTerminal();
         channel = session.createChannel("shell");
                channel.setIn(new NoCloseInputStream(System.in));
                ((ChannelShell) channel).setupSensibleDefaultPty();
            }
            channel.setOut(AnsiConsole.wrapOutputStream(System.out));
            channel.setErr(AnsiConsole.wrapOutputStream(System.err));
View Full Code Here


        }

        log.debug("Connected to {}:{}", getHost(), getPort());

        AuthFuture authResult;
        ClientSession session = connectFuture.getSession();

        KeyPairProvider keyPairProvider;
        final String certResource = getCertResource();
        if (certResource != null) {
            log.debug("Attempting to authenticate using ResourceKey '{}'...", certResource);
            keyPairProvider = new ResourceHelperKeyPairProvider(new String[]{certResource}, getCamelContext().getClassResolver());
        } else {
            keyPairProvider = getKeyPairProvider();
        }

        if (keyPairProvider != null) {
            log.debug("Attempting to authenticate username '{}' using Key...", getUsername());
            KeyPair pair = keyPairProvider.loadKey(getKeyType());
            authResult = session.authPublicKey(getUsername(), pair);
        } else {
            log.debug("Attempting to authenticate username '{}' using Password...", getUsername());
            authResult = session.authPassword(getUsername(), getPassword());
        }

        authResult.await(getTimeout());

        if (!authResult.isDone() || authResult.isFailure()) {
            log.debug("Failed to authenticate");
            throw new RuntimeCamelException("Failed to authenticate username " + getUsername());
        }

        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_EXEC, command);

        ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0});
        channel.setIn(in);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
View Full Code Here

            client = SshClient.setUpDefaultClient();
            client.setAgentFactory(new LocalAgentFactory(agent));
            client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local");
            client.start();
            int retries = 0;
            ClientSession session = null;
            do {
                ConnectFuture future = client.connect(host, port);
                future.await();
                try {
                    session = future.getSession();
                } catch (RuntimeSshException ex) {
                    if (retries++ < retryAttempts) {
                        Thread.sleep(retryDelay * 1000);
                        System.out.println("retrying (attempt " + retries + ") ...");
                    } else {
                        throw ex;
                    }
                }
            } while (session == null);
            if (!session.authAgent(user).await().isSuccess()) {
                if (password == null) {
                    Console console = System.console();
                    if (console != null) {
                        char[] readPassword = console.readPassword("Password: ");
                        if (readPassword != null) {
                            password = new String(readPassword);
                        }
                    } else {
                        throw new Exception("Unable to prompt password: could not get system console");
                    }
                }
                if (!session.authPassword(user, password).await().isSuccess()) {
                    throw new Exception("Authentication failure");
                }
            }
            ClientChannel channel;
      if (sb.length() > 0) {
                channel = session.createChannel("exec", sb.append("\n").toString());
                channel.setIn(new ByteArrayInputStream(new byte[0]));
      } else {
                terminal = new TerminalFactory().getTerminal();
         channel = session.createChannel("shell");
                ConsoleInputStream in = new ConsoleInputStream(terminal.wrapInIfNeeded(System.in));
                new Thread(in).start();
                channel.setIn(in);
                ((ChannelShell) channel).setupSensibleDefaultPty();
                ((ChannelShell) channel).setAgentForwarding(true);
View Full Code Here

    @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();
View Full Code Here

            client = SshClient.setUpDefaultClient();
            client.setAgentFactory(new LocalAgentFactory(agent));
            client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local");
            client.start();
            int retries = 0;
            ClientSession session = null;
            do {
                ConnectFuture future = client.connect(host, port);
                future.await();
                try {
                    session = future.getSession();
                } catch (RuntimeSshException ex) {
                    if (retries++ < retryAttempts) {
                        Thread.sleep(retryDelay * 1000);
                        System.out.println("retrying (attempt " + retries + ") ...");
                    } else {
                        throw ex;
                    }
                }
            } while (session == null);
            if (!session.authAgent(user).await().isSuccess()) {
                if (password == null) {
                    Console console = System.console();
                    if (console != null) {
                        char[] readPassword = console.readPassword("Password: ");
                        if (readPassword != null) {
                            password = new String(readPassword);
                        }
                    } else {
                        throw new Exception("Unable to prompt password: could not get system console");
                    }
                }
                if (!session.authPassword(user, password).await().isSuccess()) {
                    throw new Exception("Authentication failure");
                }
            }
            ClientChannel channel;
      if (sb.length() > 0) {
                channel = session.createChannel("exec", sb.append("\n").toString());
                channel.setIn(new ByteArrayInputStream(new byte[0]));
      } else {
                terminal = new TerminalFactory().getTerminal();
         channel = session.createChannel("shell");
                ConsoleInputStream in = new ConsoleInputStream(terminal.wrapInIfNeeded(System.in));
                new Thread(in).start();
                channel.setIn(in);
                ((ChannelShell) channel).setPtyColumns(terminal != null ? terminal.getWidth() : 80);
                ((ChannelShell) channel).setupSensibleDefaultPty();
View Full Code Here

        Terminal terminal = null;
        try {
            client = SshClient.setUpDefaultClient();
            setupAgent(config.getUser(), client);
            client.start();
            ClientSession session = connectWithRetries(client, config);
            if (!session.authAgent(config.getUser()).await().isSuccess()) {
                String password = null;
                Console console = System.console();
                if (console != null) {
                    char[] readPassword = console.readPassword("Password: ");
                    if (readPassword != null) {
                        password = new String(readPassword);
                    }
                } else {
                    throw new Exception("Unable to prompt password: could not get system console");
                }
                if (!session.authPassword(config.getUser(), password).await().isSuccess()) {
                    throw new Exception("Authentication failure");
                }
            }
            ClientChannel channel;
            if (config.getCommand().length() > 0) {
                channel = session.createChannel("exec", config.getCommand() + "\n");
                channel.setIn(new ByteArrayInputStream(new byte[0]));
            } else {
                TerminalFactory.registerFlavor(TerminalFactory.Flavor.UNIX, NoInterruptUnixTerminal.class);
                terminal = TerminalFactory.create();
                channel = session.createChannel("shell");
                ConsoleInputStream in = new ConsoleInputStream(terminal.wrapInIfNeeded(System.in));
                new Thread(in).start();
                channel.setIn(in);
                ((ChannelShell) channel).setupSensibleDefaultPty();
                ((ChannelShell) channel).setAgentForwarding(true);
View Full Code Here

        client.setAgentFactory(new LocalAgentFactory(agent));
        client.getProperties().put(SshAgent.SSH_AUTHSOCKET_ENV_NAME, "local");
    }

    private static ClientSession connectWithRetries(SshClient client, ClientConfig config) throws Exception, InterruptedException {
        ClientSession session = null;
        int retries = 0;
        do {
            ConnectFuture future = client.connect(config.getHost(), config.getPort());
            future.await();
            try {
View Full Code Here

    //
    SshClient client = SshClient.setUpDefaultClient();
    client.start();

    //
    ClientSession session = client.connect("localhost", port).await().getSession();
    session.authPassword("root", "");

    //
    ChannelShell channel = (ChannelShell)session.createShellChannel();
    channel.setPtyModes(tty);

    //
    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream channelIn = new PipedInputStream(out);
View Full Code Here

        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 {
                    session = future.getSession();
                } catch (RuntimeSshException ex) {
                    if (retries++ < retryAttempts) {
                        Thread.sleep(retryDelay * 1000);
                        System.out.println("retrying (attempt " + retries + ") ...");
                    } else {
                        throw ex;
                    }
                }
            } while (session == null);
            if (!session.authPassword(user, password).await().isSuccess()) {
                throw new Exception("Authentication failure");
            }
            ClientChannel channel;
      if (sb.length() > 0) {
         channel = session.createChannel("exec");
              channel.setIn(new ByteArrayInputStream(sb.append("\n").toString().getBytes()));
      } else {
                terminal = new TerminalFactory().getTerminal();
         channel = session.createChannel("shell");
                channel.setIn(System.in);
                ((ChannelShell) channel).setupSensibleDefaultPty();
            }
            channel.setOut(AnsiConsole.wrapOutputStream(System.out));
            channel.setErr(AnsiConsole.wrapOutputStream(System.err));
View Full Code Here

        }

        log.debug("Connected to {}:{}", getHost(), getPort());

        AuthFuture authResult;
        ClientSession session = connectFuture.getSession();

        KeyPairProvider keyPairProvider;
        final String certResource = getCertResource();
        if (certResource != null) {
            log.debug("Attempting to authenticate using ResourceKey '{}'...", certResource);
            keyPairProvider = new ResourceHelperKeyPairProvider(new String[]{certResource}, getCamelContext().getClassResolver());
        } else {
            keyPairProvider = getKeyPairProvider();
        }

        if (keyPairProvider != null) {
            log.debug("Attempting to authenticate username '{}' using Key...", getUsername());
            KeyPair pair = keyPairProvider.loadKey(getKeyType());
            authResult = session.authPublicKey(getUsername(), pair);
        } else {
            log.debug("Attempting to authenticate username '{}' using Password...", getUsername());
            authResult = session.authPassword(getUsername(), getPassword());
        }

        authResult.await(getTimeout());

        if (!authResult.isDone() || authResult.isFailure()) {
            log.debug("Failed to authenticate");
            throw new RuntimeCamelException("Failed to authenticate username " + getUsername());
        }

        ClientChannel channel = session.createChannel(ClientChannel.CHANNEL_EXEC, command);

        ByteArrayInputStream in = new ByteArrayInputStream(new byte[]{0});
        channel.setIn(in);

        ByteArrayOutputStream out = new ByteArrayOutputStream();
View Full Code Here

TOP

Related Classes of org.apache.sshd.ClientSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.