2017-12-14 11 views
1

SSH 프로토콜을 사용하는 원격 시스템에서 서비스를 실행하려고합니다. Ganymed SSH-2 Java 라이브러리 (버전 262) 사용하고 단일 명령을 실행하려면 잘 작동하지만 단일 세션 동안 여러 명령을 실행하는 동안 몇 가지 어려움을 직면했습니다.Session :: execCommand() 호출 후 IOException이 발생하는 이유는 무엇입니까?

왜 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

가 그것은 ch.ethz.ssh2.Session

세션 자바 독에 언급되어있는 프로그램의 원격 실행된다. "프로그램"은이 컨텍스트에서 셸, 응용 프로그램 또는 시스템 명령을 의미합니다. [....] 한 세션에서 하나의 프로그램 만 시작할 수 있습니다. 그러나 여러 세션을 동시에 활성화 할 수 있습니다.

나는 당신이이 같은 예를에 대해 여러 세션, 를 사용한다고 생각 :

private static void executeShellCommands(Connection connection, List<String> commandList) throws Exception { 
    for(String command : commandList) { 
     Session session = connection.openSession(); 
     InputStream stdout = new StreamGobbler(session.getStdout()); 
     session.requestDumbPTY(); 
     session.startShell(); 
     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(); 
    } 
}