Java 无法使用jsch运行top命令
我无法使用JSch运行top命令。请参阅以下代码:Java 无法使用jsch运行top命令,java,jsch,Java,Jsch,我无法使用JSch运行top命令。请参阅以下代码: Session session = null; ChannelShell channel = null; PipedInputStream pipeIn = null; PipedOutputStream pipeOut = null; String response = ""; try { session = getSession(hostIp, userName, password);
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命令的输出,但没有显示它的任何响应 您没有显示分配给
命令
变量的值,但当我将分配给它“top\n”
并运行代码时,我可以看到输出。你是不是错过了命令后的换行符?在写入pipeOut
流之后,您可能还必须刷新它,尽管在我的测试中这不是必需的