我正在尝试使用SSH协议在远程计算机上运行服务。我使用Ganymed SSH-2用于Java库(版本262)并且执行单个命令它工作正常,但是在单个会话期间尝试执行多个命令时遇到了一些困难。
你能帮我解决一下为什么会引发IOException以及如何解决它?
请在下面的摘录中找到代码:
private static void executeShellCommands(Connection connection, List<String> commandList) throws Exception {
Session session = connection.openSession();
InputStream stdout = new StreamGobbler(session.getStdout());
session.requestDumbPTY();
session.startShell();
for(String command : commandList) {
// The next line throws java.io.IOException
session.execCommand(command);
try (BufferedReader br = new BufferedReader(new InputStreamReader(stdout))) {
String line = br.readLine() + "\n";
StringBuilder shellOutput = new StringBuilder();
while (line != null) {
shellOutput.append(line);
line = br.readLine() + "\n";
}
}
}
session.close();
}
堆栈跟踪是:
java.io.IOException: A remote execution has already started.
at ch.ethz.ssh2.Session.execCommand(Session.java:282)
at ch.ethz.ssh2.Session.execCommand(Session.java:260)
at com.myproject.test.ssh.util.SshOperations.executeShellCommands(SshOperations.java:124)
at com.myproject.test.ssh.util.SshOperations.runBatchProcessingService(SshOperations.java:147)
at com.myproject.test.ssh.step.definitions.DocumentBatchProcessingStepDefs.testFileIsCopiedToBpsViaSSH(DocumentBatchProcessingStepDefs.java:97)
at ✽.Given test file is copied to BPS via SSH (myrepo/src/test/resources/cucumber/Regression.feature:21)
提前谢谢。
答案 0 :(得分:0)
在AmazonDynamoDBClientBuilder.standard().build()
会话是程序的远程执行。 “程序”在这种情况下意味着shell,应用程序或系统命令[....] 只能在会话中启动一个程序。但是,多个会话可以同时处于活动状态。
我认为您应该使用多个会话,例如,如下所示:
ch.ethz.ssh2.Session