问题
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