Java通過SSH2協議運行遠程程式

來源:互聯網
上載者:User

我使用Ganymed庫,訪問http://www.ganymed.ethz.ch/ssh2/網頁,下載最新版本的程式,解壓後,在NetBeans下如下設定。

然後在我們的Java應用程式中,加入Ganymed-ssh2-build2庫。

假定我在192.168.0.110的UBuntu Server 9.04系統的/home/chenshu目錄下放了一個檔案夾test,test檔案夾對應測試Java類的包名test,我們在該機器上運用javac ./test/Main.java編譯後,在另一台機器上運行Java程式,該程式通過調用我們建立的Ganymed-ssh2-build2庫的類完成遠程調用功能:

package javaapplication3;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.ConnectionInfo;
import ch.ethz.ssh2.Session;
/**
 *
 * @author chenshu
 */
public class Main {
    public static void main(String[] args) {
        try {
            Connection con = new Connection("192.168.0.110");
            ConnectionInfo info = con.connect();
            boolean result = con.authenticateWithPassword("chenshu", "770328");
            Session session = con.openSession();
            session.execCommand("java test.Main");
        } catch (Exception ex) {
            System.out.println(ex.getLocalizedMessage());
        }
    }
}

現在看看伺服器上,多了一個test.txt,那是test.Main類運行後產生的檔案。你們可以隨便放一個Java測試程式,這裡test程式碼已經無關緊要了。
搞定!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.