2011-10-20 59 views
0

我正在通過java程序與我的linux服務器進行SSH通信,我能夠執行諸如shell腳本之類的命令。但問題是,每當我執行一個特定的shell腳本時,它會給出一個選項來選擇yes還是no,現在從我的java控制檯中選擇yes,但這實際上並沒有與linux服務器交互。如何通過java編程來完成這項工作?如果你有任何代碼請分享。我正在修改我的SSH代碼。無法通過SSH從JAVA程序進行交互

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。無論你在這個問題上應該這樣說。但是,如果shell腳本執行某些操作,以便從控制檯獲得輸入而不是stdin,則無法對其執行任何操作。 – EJP

+0

是的我使用ganymade ssh,但如何使它與linux服務器交互? – user993158

回答

-2

您可以使用「是」命令。從Yes輸入管道到下一個命令

yes | apt-get install some_software 
+0

對不起,但沒有得到你的想法..請給所有命令字符串的前綴「yes |」給這個 – user993158

+0

需要編碼。 yes命令輸出「y」並用|我們可以將此輸出作爲輸入發送到下一個命令。所以當你執行一個命令並等待輸入時,它會從yes命令中獲得。 它可能工作。 –

+1

對不起,它不會工作,因爲你沒有使用shell,而是執行命令。啓動一個shell會打開一整個蠕蟲。最簡單的方法可能是編寫一個可以執行你想要的命令的shell腳本,以及使用ssh執行* * – KarlP

1

您沒有寫入任何內容到輸入流。只要在Java控制檯中輸入就沒有任何作用。

創建會話標準輸入-的OutputStream一個PrintWriter:

然後你就可以out.println("y"),如果你需要。如果您需要使用java控制檯進行交互,則需要從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

雖然有警告。使用ganymede和ssh,unix會話等是非常棘手的,你真的需要了解一切。 – KarlP