Search in sources :

Example 1 with OutputAsStringExecuteStreamHandler

use of com.jn.langx.commandline.streamhandler.OutputAsStringExecuteStreamHandler in project agileway by fangjinuo.

the class SshCommandLineExecutorTest method showResult.

private static void showResult(SshCommandLineExecutor executor) {
    DefaultExecuteResultHandler resultHandler = (DefaultExecuteResultHandler) executor.getResultHandler();
    if (resultHandler.hasResult()) {
        Throwable exception = resultHandler.getException();
        if (exception != null) {
            logger.error(exception.getMessage(), exception);
        } else {
            OutputAsStringExecuteStreamHandler output = (OutputAsStringExecuteStreamHandler) executor.getStreamHandler();
            String str = output.getOutputContent();
            logger.info(str);
        }
    }
}
Also used : OutputAsStringExecuteStreamHandler(com.jn.langx.commandline.streamhandler.OutputAsStringExecuteStreamHandler) DefaultExecuteResultHandler(com.jn.langx.commandline.DefaultExecuteResultHandler)

Example 2 with OutputAsStringExecuteStreamHandler

use of com.jn.langx.commandline.streamhandler.OutputAsStringExecuteStreamHandler in project agileway by fangjinuo.

the class SshCommandLineExecutorTest method testExec.

private void testExec(SshConnectionFactory connectionFactory, AbstractSshConnectionConfig connectionConfig) throws SshException, IOException {
    // connectionConfig.setHost("192.168.234.128");
    connectionConfig.setHost("192.168.1.70");
    connectionConfig.setPort(22);
    connectionConfig.setUser("fangjinuo");
    connectionConfig.setPassword("fjn13570");
    SshConnection connection = connectionFactory.get(connectionConfig);
    SshCommandLineExecutor executor = new SshCommandLineExecutor(connection);
    executor.setWorkingDirectory(new File("~/.java"));
    OutputAsStringExecuteStreamHandler output = new OutputAsStringExecuteStreamHandler();
    executor.setStreamHandler(output);
    executor.execute(CommandLine.parse("ifconfig"));
    showResult(executor);
    System.out.println("====================================");
    executor.execute(CommandLine.parse("ls -al"));
    showResult(executor);
    connection.close();
}
Also used : OutputAsStringExecuteStreamHandler(com.jn.langx.commandline.streamhandler.OutputAsStringExecuteStreamHandler) SshConnection(com.jn.agileway.ssh.client.SshConnection) SshCommandLineExecutor(com.jn.agileway.ssh.client.supports.command.executor.SshCommandLineExecutor) File(java.io.File)

Aggregations

OutputAsStringExecuteStreamHandler (com.jn.langx.commandline.streamhandler.OutputAsStringExecuteStreamHandler)2 SshConnection (com.jn.agileway.ssh.client.SshConnection)1 SshCommandLineExecutor (com.jn.agileway.ssh.client.supports.command.executor.SshCommandLineExecutor)1 DefaultExecuteResultHandler (com.jn.langx.commandline.DefaultExecuteResultHandler)1 File (java.io.File)1