java實現連接遠程服務器並執行命令的基本原理


 

一、所需jar包

    需要借助Ganymed SSH的jar包:  ganymed-ssh2-build210.jar    

   下載地址: http://www.ganymed.ethz.ch/ssh2/

   API詳情:  http://www.ganymed.ethz.ch/ssh2/javadoc/ch/ethz/ssh2/package-summary.html

 

二、實現原理 

  Ganymed SSH-2 java在整個訪問過程中擔當SSH的客戶端,由於Linux系統自帶SSH服務,所以可以直接訪問Linux系統並執行相關命令,而 Windows系統則需要首先安裝SSH服務。

 

三、Win下SSH服務的安裝配置

    當遠程服務器為Windows系統時, 需要在遠程服務器中安裝Windows版的SSH服務,比如:freesshd。

    freesshd下載地址: http://www.freesshd.com/?ctt=download

    freesshd安裝與配置:(可以參考:http://www.cnblogs.com/xred/archive/2012/04/21/2461627.html

         1.安裝完freesshd后,首選在[Users]下添加用來遠程連接的win系統用戶,此處采用密碼認證的方式,允許使用shell:

        2.然后再在【Authentication】下設置允許密碼認證方式:

      3.到[Server status]下查看SSH服務器狀態,確保啟動即可。最后點擊【確定】即可。

 

四、java代碼實現遠程連接服務器並執行命令

/*  * To change this license header, choose License Headers in Project Properties.  * To change this template file, choose Tools | Templates  * and open the template in the editor.  */ package test; import java.io.IOException; import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.Session; import ch.ethz.ssh2.StreamGobbler; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; /*   @author: Liu Yuanyuan   purpose: test connecting remote computer and execute linux command */ public class TestRemoteConnect  {     public static void main(String[] args)      {         String hostname = "192.168.100.50";         int port = 22;//22 usually the default port         String username = "root";         String password = "highgo";         //指明連接主機的IP地址           Connection conn = new Connection(hostname,port);         Session ssh = null;         try         {             //連接到主機               conn.connect();             //使用用戶名和密碼校驗               boolean isconn = conn.authenticateWithPassword(username, password);             if (!isconn)              {                 System.out.println("用戶名稱或者是密碼不正確");             }              else      {                 System.out.println("已經連接OK");                                  //以下是linux的示例                 //將本地conf/server_start.sh傳輸到遠程主機的/opt/pg944/目錄下                 SCPClient clt = conn.createSCPClient();                                 clt.put("conf/server_start.sh", "/opt/pg944/");                                                  //執行命令                 ssh = conn.openSession();                  ssh.execCommand("sh /root/hello.sh");                 //ssh.execCommand("perl /root/hello.pl");                   //只允許使用一行命令,即ssh對象只能使用一次execCommand這個方法,多次使用則會出現異常.                      //使用多個命令用分號隔開                   //ssh.execCommand("cd /root; sh hello.sh");          /* 執行windows系統命令的示例         Session sess = conn.openSession();                 sess.execCommand("ipconfig");                 */ //將Terminal屏幕上的文字全部打印出來                                   InputStream is = new StreamGobbler(ssh.getStdout());                 BufferedReader brs = new BufferedReader(new InputStreamReader(is));                 while (true)  {                     String line = brs.readLine();                     if (line == null)              {                         break;                     }                     System.out.println(line);                 }             }                     }          catch (IOException e)          {             System.out.println(e.getMessage());             e.printStackTrace();         }          finally  {             //連接的Session和Connection對象都需要關閉               if(ssh!=null)             {                 ssh.close();             }             if(conn!=null)             {                 conn.close();             }         }     } }

 

五、其他的實現方式:使用jsch 遠程連接linux執行命令(與以上類似,未測試)

   jar包下載:http://www.jcraft.com/jsch/

   實現參考:http://blog.csdn.net/allen_zhao_2012/article/details/7941631


免責聲明!

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



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