所以我使用Jsch在远程服务器上运行多个命令。目前我打开一个会话,每当我需要运行远程命令时,我打开新的exec通道,执行命令,并断开连接。
然而,我经常遇到异常(虽然并非总是如此)。我想知道造成这种异常的原因。是因为我断开连接太快了吗?或者我应该重新使用exec频道而不是每次都创建新频道?
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
/// some code goes here ///
channel.connect(); // <---- No Exception here
while (true) {
if (channel.isClosed()) {
exitCode = channel.getExitStatus();
LOGGER.info("Exit code = " + exitCode);
if (exitCode != 0) {
throw new IOException("Command failed with code " + exitCode + " error = " + getError());
}
break;
}
Thread.sleep(1000);
}
channel.disconnect();
//// some code goes here ////
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
/// some code goes here ///
channel.connect(); // <---- Sometimes exception here (JschException: channel is not opened)
异常追踪:
if(this.getRecipient()==-1){ // timeout
throw new JSchException("channel is not opened.");
}
非常感谢任何帮助!