Search in sources :

Example 1 with SessionedChannel

use of com.jn.agileway.ssh.client.channel.SessionedChannel in project agileway by fangjinuo.

the class SshCommandTest method executeAndDump.

private void executeAndDump(SshConnection connection, String command) throws JSchException, IOException {
    logger.info("\n====== start execute: {} ======", command);
    SessionedChannel channel = connection.openSession();
    channel.exec(command);
    // System.out.println(channel.getExitStatus());
    InputStream outStream = channel.getInputStream();
    String content = IOs.readAsString(outStream);
    logger.info(content);
    channel.close();
    logger.info("====== finish execute: {} ======", command);
}
Also used : InputStream(java.io.InputStream) SessionedChannel(com.jn.agileway.ssh.client.channel.SessionedChannel)

Example 2 with SessionedChannel

use of com.jn.agileway.ssh.client.channel.SessionedChannel in project agileway by fangjinuo.

the class SshCommandLineLauncher method exec.

@Override
public SshCommandExecutionAdaptor exec(CommandLine commandLine, Map<String, String> environmentVariables, File workingDirectory) throws IOException {
    try {
        if (!connection.isConnected()) {
            throw new SshException(new IllegalStateException("connection is not connected"));
        }
        final SessionedChannel sessionChannel = connection.openSession();
        Preconditions.checkNotNull(sessionChannel, "the ssh exec session channel is null");
        String command = commandLine.getCommandLineString();
        if (workingDirectory != null) {
            String path = workingDirectory.getPath();
            path = path.replace("\\", "/");
            command = "cd " + path + ";" + command;
        }
        if (Emptys.isNotEmpty(environmentVariables)) {
            String envs = null;
            if (environmentSettingsSupplier != null) {
                envs = environmentSettingsSupplier.get(environmentVariables);
            }
            if (Strings.isNotEmpty(envs)) {
                command = envs + command;
            } else {
                Collects.forEach(environmentVariables, new Consumer2<String, String>() {

                    @Override
                    public void accept(String variable, String value) {
                        sessionChannel.env(variable, value);
                    }
                });
            }
        }
        sessionChannel.exec(command);
        return new SshCommandExecutionAdaptor(sessionChannel);
    } catch (Throwable ex) {
        throw Throwables.wrapAsRuntimeException(ex);
    }
}
Also used : SessionedChannel(com.jn.agileway.ssh.client.channel.SessionedChannel) SshException(com.jn.agileway.ssh.client.SshException)

Example 3 with SessionedChannel

use of com.jn.agileway.ssh.client.channel.SessionedChannel in project agileway by fangjinuo.

the class SshClients method exec.

public static SshCommandResponse exec(@NonNull SshConnection connection, @Nullable Map<String, String> environmentVariables, @Nullable Supplier<Map<String, String>, String> environmentSettingsSupplier, @Nullable String workingDirectory, @NonNull String command, @Nullable String encoding) throws SshException {
    Preconditions.checkState(connection != null && connection.isConnected() && !connection.isClosed(), "connection status invalid");
    Preconditions.checkNotEmpty(command, "the command is not supplied");
    Charset charset = Charsets.UTF_8;
    if (Strings.isNotEmpty(encoding)) {
        try {
            charset = Charsets.getCharset(encoding);
        } catch (Throwable ex) {
            logger.warn("The encoding is invalid : {}", encoding);
        }
    }
    final SessionedChannel channel = connection.openSession();
    if (Strings.isNotEmpty(workingDirectory)) {
        workingDirectory = workingDirectory.replace("\\", "/");
        command = "cd " + workingDirectory + ";" + command;
    }
    if (Emptys.isNotEmpty(environmentVariables)) {
        String envs = null;
        if (environmentSettingsSupplier != null) {
            envs = environmentSettingsSupplier.get(environmentVariables);
        }
        if (Strings.isNotEmpty(envs)) {
            command = envs + command;
        } else {
            Collects.forEach(environmentVariables, new Consumer2<String, String>() {

                @Override
                public void accept(String variable, String value) {
                    channel.env(variable, value);
                }
            });
        }
    }
    channel.exec(command);
    int exitStatus = channel.getExitStatus();
    SshCommandResponse response = new SshCommandResponse();
    response.setExitStatus(exitStatus);
    try {
        if (exitStatus != 0) {
            InputStream errorInputStream = channel.getErrorInputStream();
            byte[] errorContent = IOs.toByteArray(errorInputStream);
            String error = new String(errorContent, charset);
            response.setExitErrorMessage(error);
        } else {
            InputStream inputStream = channel.getInputStream();
            // if (inputStream.available() > 0) { // 这行代码可以避免阻塞,但又容易导致某些ssh 库获取不到任何内容
            byte[] bytes = IOs.toByteArray(inputStream);
            String content = new String(bytes, charset);
            response.setResult(content);
        // }
        }
    } catch (Throwable ex) {
        logger.error(ex.getMessage(), ex);
    }
    return response;
}
Also used : SshCommandResponse(com.jn.agileway.ssh.client.supports.command.SshCommandResponse) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) SessionedChannel(com.jn.agileway.ssh.client.channel.SessionedChannel)

Aggregations

SessionedChannel (com.jn.agileway.ssh.client.channel.SessionedChannel)3 InputStream (java.io.InputStream)2 SshException (com.jn.agileway.ssh.client.SshException)1 SshCommandResponse (com.jn.agileway.ssh.client.supports.command.SshCommandResponse)1 Charset (java.nio.charset.Charset)1