Java使用SSH遠程訪問Windows並執行命令


轉載至:http://blog.csdn.net/carolzhang8406/article/details/6760430

windows由於沒有默認的ssh server,因此在允許ssh之前需要先安裝ssh server。

下載freeSSHd

http://www.freesshd.com/?ctt=download

安裝
直接執行freeSSHd.exe就可以進行安裝了(用戶一定要有管理員權限),安裝過程中freeSSHd會問你

Private keys should be created. Should I do it now?

這是問你是否需要現在創建私鑰,回答是

Do you want to run FreeSSHd as a system service?
這是問你是否希望把freeSSHd作為系統服務啟動,回答是之后就安裝完成了。

安裝完成之后,雙擊freesshd圖標(桌面或開始菜單),不會直接打開配置界面,而是需要在任務欄的“顯示隱藏圖標”(正三角圖標)處右鍵freessh圖標,選擇setting。

配置用戶名和密碼:(java代碼中連接的用戶名和密碼)

 

 

 

檢查freesshd server status狀態。切換至“server status”標簽頁,查看“SSH server is running”是否打鈎,如果沒有,點擊下方連接檢查,如果報錯“the specified address is already in use”,則是由於安裝時詢問“Do you want to run FreeSSHd as a system service?”選擇了“是”導致的,只需要在services.msc中將該服務停掉,再在配置界面啟動,顯示為打鈎狀態即可。

 

java代碼如下:

[java]  view plain  copy
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5.   
  6. import ch.ethz.ssh2.Connection;  
  7. import ch.ethz.ssh2.Session;  
  8. import ch.ethz.ssh2.StreamGobbler;  
  9.   
  10. public class SSHWindows {  
  11.   
  12.     public static void main(String[] args) {  
  13.         // TODO Auto-generated method stub  
  14.          String hostname ="192.168.30.10";  
  15.             String username="administrator";  
  16.             String password="Talent123";  
  17.             try{  
  18.                 //建立連接  
  19.                 Connection conn= new Connection(hostname);  
  20.            //     System.out.println("set up connections");  
  21.                 conn.connect();  
  22.                 //利用用戶名和密碼進行授權  
  23.                 boolean isAuthenticated = conn.authenticateWithPassword(username, password);  
  24.                 if(isAuthenticated ==false)  
  25.                 {  
  26.            //       System.out.println("--------");  
  27.                     throw new IOException("Authorication failed");  
  28.                 }  
  29.                 //打開會話  
  30.                 Session sess = conn.openSession();  
  31.             //    System.out.println("cmd----");  
  32.                 //執行命令  
  33.                 sess.execCommand("ruby C:\\WhatWeb-master\\whatweb --output-xml http://216.139.147.75:443/");  
  34.            //     System.out.println("The execute command output is:");  
  35.                 InputStream stdout = new StreamGobbler(sess.getStdout());  
  36.                 BufferedReader br = new BufferedReader(new InputStreamReader(stdout));  
  37.                 while(true)  
  38.                 {  
  39.                     String line = br.readLine();  
  40.                     if(line==null) break;  
  41.                     System.out.println(line);  
  42.                 }  
  43.              //   System.out.println("Exit code "+sess.getExitStatus());  
  44.                 sess.close();  
  45.                 conn.close();  
  46.            //     System.out.println("Connection closed");  
  47.                   
  48.             }catch(IOException e)  
  49.             {  
  50.                 System.out.println("can not access the remote machine");  
  51.             }  
  52.         }  
  53.   
  54.   
  55. }  



以上代碼依賴於ssh2的jar包。下載地址:http://www.ganymed.ethz.ch/ssh2/

注意: 該服務端需安裝在數據庫所在服務器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM