Unable to run top command using jsch

时光毁灭记忆、已成空白 提交于 2020-01-24 18:15:55

问题


I am unable to run top command using JSch. Please see following code:

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);
    }

Since top is an interactive command I have used ChannelShell. The above code shows the output up to top command but it not showing any response of it.


回答1:


You are not showing the value that you're assigning to the command variable, but when I assign "top\n" to it and then run your code, I can see the output. Are you maybe missing the newline after the command? You may also have to flush the pipeOut stream after writing to it, although that was not necessary in my tests.



来源:https://stackoverflow.com/questions/30502471/unable-to-run-top-command-using-jsch

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!