2011-10-20 3 views
0

Java 프로그램을 통해 내 Linux 서버에서 SSH를 수행하고 있는데, 쉘 스크립트와 같은 명령을 실행할 수 있습니다. 하지만 문제는 내가 예 또는 아니요 중에서 선택할 수있는 옵션을 제공하는 특정 셸 스크립트를 실행할 때마다입니다. 이제는 Java 콘솔에서 예를 선택하고 있지만 실제로는 Linux 서버와 상호 작용하지 않습니다. 자바 프로그래밍을 통해이 직업을 수행하는 방법? 코드 Spake가 있다면 공유하십시오. 나는 SSH를하는 것에 대한 나의 코드를 줄이고있다.Java 프로그램에서 SSH를 통해 상호 작용할 수 없습니다.

String arr1[]=new String[arr.length]; 
    ReadConfig rc=new ReadConfig(); 
    rc.readConfig(); 
    hostname=rc.hostname; 
    username=rc.username; 
    password=rc.password; 
    for(int i=0;i<arr.length;i++) 
    { 
     arr1[i]= arr[i]; 
     System.out.println("the array value "+arr1[i]+" "+i); 
    } 

    try{ 
     String com=arr1[5].toString().trim(); 
     System.out.println(" "+com); 
     /* Create a connection instance */ 

     conn = new Connection(hostname); 

     /* Now connect */ 
       conn.connect(); 


       /* Authenticate. 
     * If you get an IOException saying something like 
     * "Authentication method password not supported by the server at this stage." 
     * then please check the FAQ. 
     */ 

      boolean isAuthenticated = conn.authenticateWithPassword(username, password); 


       if (isAuthenticated == false) 
        throw new IOException("Authentication failed."); 

       /* Create a session */ 

       sess = conn.openSession(); 
       try{ 
        sess.execCommand(com); 


       //obj.append("Pass"); 
       }catch(Exception e){ 


        //obj.append("Fail"); 
       } 
       //Session sess1 = conn.openSession(); 
       System.out.println("Here is some information about the remote host:"); 
       InputStream stdout = new StreamGobbler(sess.getStdout()); 
       BufferedReader br = new BufferedReader(new InputStreamReader(stdout)); 

       while (true) 
       { 
        String line = br.readLine(); 
        if (line == null) 
         break; 
        System.out.println(line); 
       } 
       /* Show exit status, if available (otherwise "null") */ 

       System.out.println("ExitCode: " + sess.getExitStatus()); 

    }catch (Exception e) { 
     // TODO: handle exception 
    }finally{ 
     /* Close this session */ 
    //obj.close(); 
     sess.close(); 
     /* Close the connection */ 

     conn.close(); 
    } 
+0

Ganymed-SSH2를 사용하는 것 같습니다. 그것이 무엇이든 당신은 질문에서 그렇게 말해야합니다. 그러나 셸 스크립트가 콘솔에서 입력을 얻으려고 무언가를 수행하면 실제로는 아무 것도 할 수 없습니다. – EJP

+0

예 ganymade ssh를 사용하고 있지만 Linux 서버와 대화식으로 만드는 방법은 무엇입니까? – user993158

답변

-2

"예"명령을 사용할 수 있습니다. 예에서 다음 명령으로 파이프 출력

yes | apt-get install some_software 
+0

죄송 합니다만 귀하의 아이디어를 얻지 못했습니다 .. 모든 명령 문자열에이 – user993158

+0

접두어 "예 |"를 요구하십시오. yes 명령은 "y"를 출력하고 | 이 출력을 다음 명령의 입력으로 보낼 수 있습니다. 그래서 언제까지 u 명령을 실행하고 그것은 입력을 기다리는, 그것은 예 명령에서 가져옵니다. 사용할 수 있습니다. –

+1

죄송합니다. 쉘을 사용하고 있지 않으므로 작동하지 않습니다. 오히려 명령을 실행 중입니다. 셸을 시작하면 다른 웜을 열 수 있습니다. 가장 간단한 방법은 아마도 당신이 원하는 것을하는 셸 스크립트를 쓰는 것이며, ssh를 사용하여 * 실행한다.) – KarlP

1

입력 스트림에는 아무 것도 쓰지 않습니다. 자바 콘솔에 입력하는 것만으로는 아무 것도하지 않습니다.

그런 다음 당신이 out.println("y")는 당신이 필요로 할 수있는 경우 :

는 세션 표준 입력-의 OutputStream의 PrintWriter를 작성합니다. java-console을 사용하여 상호 작용이 필요한 경우 System.in에서 읽고 출력 스트림으로 보내야합니다.

PrintWriter out = createPW(new OutputStreamWriter(sess.getStdin()), true); 

다음은 적절한 줄 구분 기호로 된 PrintWriter를 만드는 방법입니다.

public static PrintWriter createPrintWriter(OutputStreamWriter out, boolean autoflush) { 
    Properties props = System.getProperties(); 
    synchronized (props) { 
     Object old = null; 
     try { 
      old = props.setProperty("line.separator", "\n"); 
      return new PrintWriter(out, autoflush); 
     } finally { 
      if (old != null) { 
       props.put("line.separator", old); 
      } 
     } 
    } 
} 
+0

경고. 가니메데와 SSH, 유닉스 세션 등으로 작업하는 것이 다소 까다 롭습니다. 모든 것에 대해 조금 알 필요가 있습니다. – KarlP

관련 문제