【问题标题】:Unable to run top command using jsch无法使用 jsch 运行 top 命令
【发布时间】:2017-08-02 01:19:00
【问题描述】:

我无法使用 JSch 运行 top 命令。请看以下代码:

Session session = null;
    ChannelShell channel = null;
    PipedInputStream pipeIn = null;
    PipedOutputStream pipeOut = null;
    String response = "";
    try {
        session = getSession(hostIp, userName, password);
        channel = (ChannelShell)session.openChannel( "shell" );
        pipeIn = new PipedInputStream();
        pipeOut = new PipedOutputStream( pipeIn );
        channel.setInputStream(pipeIn);
        channel.setOutputStream(System.out, true);
        channel.connect(3*1000);
        pipeOut.write(command.getBytes());
        Thread.sleep(3*1000);



    } catch (Exception e) {
        throw e;
    } finally {
        if (pipeIn != null) pipeIn.close();
        if (pipeOut != null) pipeOut.close();
        closeResources(session, channel);
    }

由于 top 是一个交互式命令,我使用了 ChannelShell。上面的代码显示了 top 命令的输出,但没有显示任何响应。

【问题讨论】:

    标签: java jsch


    【解决方案1】:

    您没有显示分配给command 变量的值,但是当我将"top\n" 分配给它然后运行您的代码时,我可以看到输出。您是否可能在命令后缺少换行符?您可能还必须在写入 pipeOut 流后对其进行刷新,尽管在我的测试中这不是必需的。

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 2011-01-25
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多