我无法使用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。上面的代码显示了最高命令的输出,但未显示任何响应。
您没有显示要分配给command
变量的值,但是当我分配"top\n"
给变量然后运行代码时,可以看到输出。命令之后您是否可能缺少换行符?您可能还需要pipeOut
在写入流后刷新流,尽管在我的测试中这不是必需的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句