【发布时间】: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 命令的输出,但没有显示任何响应。
【问题讨论】: