【问题标题】:JSch exec sudo command : How to access sudo command output text? [duplicate]JSch exec sudo 命令:如何访问 sudo 命令输出文本? [复制]
【发布时间】:2019-05-29 23:53:37
【问题描述】:

我正在使用 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] 输出行?

【问题讨论】:

    标签: java linux jsch


    【解决方案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 }
    

    【讨论】:

    • 什么“无限循环”?你和我的代码都以channel.isClosed() 结尾。 --- 而且您的代码实际上遇到了与链接问题的错误接受答案相同的问题。如果 error output 上的输出过多,而 normal output 上没有混合输出,则您的代码将死锁。用命令"for((i=1;i&lt;=10000;i+=2)); do echo \"Long error output - $i\" &gt;&amp;2; done"试试吧。
    • 啊。也许它陷入僵局而不是进入无限循环。对不起。
    • 嗨,马丁。我在上面发布的接受的解决方案可以正常工作并退出。当我在 47554723 的解决方案中使用代码时,它挂起。没有错误输出,所以我认为这是一个无限循环或死锁。
    猜你喜欢
    • 2017-09-14
    • 2014-05-17
    • 2012-12-22
    • 2013-09-26
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2019-08-30
    相关资源
    最近更新 更多