Java runs a remote program through the SSH2 protocol

Source: Internet
Author: User

I use the Ganymed library to access ingress.

 

Then add the Ganymed-ssh2-build2 library to our Java application.

Suppose I have put a folder test under the/home/chenshu directory of UBuntu Server 9.04 of 192.168.0.110. The test folder corresponds to the test Java class package name. We use javac on this machine. /test/Main. after java compilation, run the Java program on another machine, the program completes the remote call function by calling the class of the newly created Ganymed-ssh2-build2 Library:

 
 
  1. package javaapplication3;  
  2.  
  3. import ch.ethz.ssh2.Connection;  
  4. import ch.ethz.ssh2.ConnectionInfo;  
  5. import ch.ethz.ssh2.Session;  
  6. /**  
  7.  *  
  8.  * @author chenshu  
  9.  */ 
  10. public class Main {  
  11.     public static void main(String[] args) {  
  12.         try {  
  13.             Connection con = new Connection("192.168.0.110");  
  14.             ConnectionInfo info = con.connect();  
  15.             boolean result = con.authenticateWithPassword("chenshu", "770328");  
  16.             Session session = con.openSession();  
  17.             session.execCommand("java test.Main");  
  18.         } catch (Exception ex) {  
  19.             System.out.println(ex.getLocalizedMessage());  
  20.         }  
  21.     }  

Now we can see that there is another test.txt on the server, which is the file generated after the test. Main class is run. You can release a Java test program. The test program code is irrelevant.

Done!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.