JSch exec sudo命令:如何访问sudo命令输出文本?

时间:2019-01-03 11:35:11

标签: java linux jsch

我正在使用JSch在远程linux服务器上运行sudo命令。

下面的示例显示了尝试cd进入目录并返回内容的尝试。我正在测试myDirectory不存在的用例。

public static List<String> executeExecCommand() {

  try {

    Session session = new JSch().getSession() // paraphrasing for brevity

    Channel channel - session.openChannel("exec");

    String command = "echo \"password\" | sudo -S bash -c \"cd myDirectory/ && ls -la \"";

    ((ChannelExec(channel).setCommand(command);
    channel.setInputStream(null);
    ((ChannelExec(channel).setErrStream(System.err);
    InputStream input = channel.getInputStream();
    channel.connect();
    List<String> output = new ArrayList<>();

    try {
        InputStreamReader inputReader = new InputStreamReader(input);
        BufferedReader bufferedReader = new BufferedReader(InputReader);
        String line = null;
        while (true) {
            while ((line = bufferedReader.readLine()) != null) {
                output.add(line);
                System.out.println("here is a line " + line);
            }
        if (channel.isClosed()( {
            if (input.available() > 0) {
                continue;
            }
            logger.DEBUG("exit status " + channel.getExitStatus());
            break;
        }
        bufferedReader.close();
        inputReader.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    channel.disconnect();
    session.disconnect();

    return output;

    } catch (Throwable t) {
        throw new JSchException("executeExecCommand : unable to establish SSH session " + t.toString());
    }
}

从intelliJ中的单元测试运行时,我得到的输出如下:

.
.
[sudo] password for username : bash: line 0: cd: myDirectory/: No such file or directory

2019-01-03 10:40:18 DEBUG [main] <Filename>:<linenumber> - exit status: 1
.
.

这是预料之中的,但是我的问题是我如何以编程方式访问[sudo]输出行?我看过ErrorStream,但是其中不包含在intelliJ的输出控制台上输出和显示的文本。

请注意,System.out.println("here is a line " + line);行未执行。

还请注意,从命令提示符运行时,不会输出[sudo]行:即“ mvn test”

那么,我该如何以编程方式访问[sudo]输出行?

1 个答案:

答案 0 :(得分:-1)

使用@MartinPrikil的解决方案How to read JSch command output?(但是此解决方案具有无限循环)

我接受了他的一些代码,并将其添加到代码中,如下所示:这使我可以通过变量“ errorBufferedReader”以编程方式访问错误文本

   ((ChannelExec(channel).setCommand(command);
    channel.setInputStream(null);
    ((ChannelExec(channel).setErrStream(System.err);
    InputStream input = channel.getInputStream();
    InputStream error = channel.getExtInputStream();
    channel.connect();
    List<String> output = new ArrayList<>();
    List<String> errorOutput = new ArrayList<>();

    try {
        InputStreamReader inputReader = new InputStreamReader(input);
        BufferedReader bufferedReader = new BufferedReader(inputReader);
        InputStreamReader errorReader = new InputStreamReader(error);
        BufferedReader errorBufferedReader = new BufferedReader(errorReader);
        String line = null;
        while (true) {
            while ((line = bufferedReader.readLine()) != null) {
                output.add(line);
                System.out.println("here is a line " + line);
            }
            while ((line = errorBufferedReader.readLine()) != null) {
                Output.add(line);
                System.out.println("here is an error line " + line);
            }
        if (channel.isClosed()( { etc etc }