2017-10-27 21 views
0

원격 서버에서 .csh 스크립트를 실행하기 위해 Jsch를 사용하려고합니다. 나는 cp, mv, ls와 같은 명령을 실행할 수있다. 그러나 일부 환경 변수를 내부적으로 참조하는 스크립트를 실행하려고하면 스크립트는 상태 1로 종료됩니다. exec.exec을 사용하여 실행할 때 액세스 할 수없는 script.sh 내에서 INTERNAL_ENV_VARIABLE이 참조됩니다. 이 종속성을 처리 할 exec에서 .csh 스크립트를 실행할 수있는 방법이 있습니까?jsch 라이브러리를 사용하여 .csh 스크립트를 실행할 수 없습니다. - Exit 1

exec 대신 셸을 사용하는 것은 옵션이 아닙니다. 셸을 열 때 여러 인증 수준이 있고 여러 자격 증명에 따라 우리가 개발중인 테스트 프레임 워크를 만들기 때문입니다.

명령 스크립트 디렉토리로 이동하여 스크립트를 실행하려고합니다.

util.executeCommand(session,"cd " + script directory+";"+"./script.csh"); 

는 콘솔 출력 명령을 실행

[email protected] 
INTERNAL_ENV_VARIABLE: Undefined variable. 
exit-status: 1 

방법 : 세션을 생성하는

public int executeCommand(Session session, String script) throws JSchException, IOException { 
    System.out.println("Execute Script " + script); 

    ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); 
    ((ChannelExec)channelExec).setPty(true); 
    InputStream in = channelExec.getInputStream(); 
    channelExec.setInputStream(null); 
    channelExec.setErrStream(System.err); 
    channelExec.setCommand(script); 
    channelExec.connect(); 

    byte[] tmp = new byte[1024]; 
    while (true) { 
     while (in.available() > 0) { 
      int i = in.read(tmp, 0, 1024); 
      System.out.println(channelExec.getErrStream()); 
      if (i < 0) 
       break; 
      System.out.print(new String(tmp, 0, i)); 
     } 
     if (channelExec.isClosed()) { 
      System.out.println("exit-status: " + channelExec.getExitStatus()); 

      break; 
     } 
     try { 
      Thread.sleep(1000); 
     } catch (Exception ee) { 
      System.out.println(ee); 
     } 
    } 

    channelExec.disconnect(); 
    return channelExec.getExitStatus(); 


} 

방법 ExecuteCommand를 : createSession를

public Session createSession(String user, String host, int Port, String Password) throws JSchException { 
     JSch jsch = new JSch(); 

     Session session = jsch.getSession(user, host, Port); 
     session.setConfig("StrictHostKeyChecking", "no"); 

     session.setPassword(Password); 
     session.connect(5000); 
     System.out.println(session.isConnected()); 
     return session; 
     } 
} 
+1

, 당신은 새로운 질문을 요청해야합니다. 기존 질문을 완전히 다른 것으로 바꾸고 기존 답변을 쓸모 없게 만들지 말아야합니다. [누군가 내 질문에 대답 할 때 무엇을해야합니까?] (https://stackoverflow.com/help/someone-answers) – Kenster

+0

죄송합니다. 나는 신입이다. 나는 지금까지 한 일을 깨닫는다. 그러나 나는 그 질문을 올바르게 얻으려고 노력하고 있었다. – njosep

+0

새로운 문제는 [JSch : 사용자 환경 변수를 "exec"채널에 표시하는 방법이 있습니까?] (https://stackoverflow.com/a/46350885/850848) –

답변

1
util.executeCommand(session,"cd " + script directory); 
util.executeCommand(session,"ls "+"script.csh"+" && exit 0 || exit 1"); 
util.executeCommand(session,"./script.csh"); 

이러한 명령 호출은 각각 다른 명령 호출과 독립적으로 실행됩니다. 특히, 각 명령은 동일한 작업 디렉토리 (사용자가 로그인 한 사용자의 홈 디렉토리)로 시작하며 사용자가 먼저 호출하는 cd 명령은 실행하는 다른 명령에 영향을 미치지 않습니다. 당신이 함께 문자열로 일련의 명령을 원하는 경우

, 당신을 한 번 호출을 실행해야 : 새 질문이있는 경우

util.executeCommand(session, "cd /some/directory && ./script.csh"); 
+0

고마워요. 이것이 ls 명령이 작동하지 않는 이유를 설명합니다. 그러나 스크립트는 여전히 실행되지 않습니다. 나는 그것을 반영하기 위해 질문을 편집 할 것이다. 응답 해 주셔서 감사합니다. – njosep