无法使用jsch运行top命令 [英] Unable to run top command using jsch
问题描述
我无法使用JSch运行top命令.请查看以下代码:
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);
}
由于top是交互式命令,因此我使用了ChannelShell.上面的代码显示了最高命令的输出,但未显示任何响应.
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.
推荐答案
您没有显示要分配给command
变量的值,但是当我为它分配"top\n"
然后运行代码时,我可以看到输出.命令之后您是否可能缺少换行符?您可能还需要在写入pipeOut
流后刷新它,尽管在我的测试中这不是必需的.
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.
这篇关于无法使用jsch运行top命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!