Java way to remotely connect computers and execute Linux commands via SSH2 (GO)

Source: Internet
Author: User

Java uses CH.ETHZ.SSH2 to remotely connect computers and execute Linux commands

API Details:

Jar Package: ganymed-ssh2-build210.jar:

Code implementation:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879 /* * 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. */packagetest;;importch.ethz.ssh2.Connection;importch.ethz.ssh2.Session;import ch.ethz.ssh2.StreamGobbler;;;;/*  @author: Liu Yuanyuan  purpose: test connecting remote computer and execute linux command*/ publicclassTestRemoteConnect {    publicstaticvoidmain(String[] args) {        String hostname = "";        String username = "root";        String password = "highgo";        //指明连接主机的IP地址          Connection conn = newConnection(hostname);        Session ssh = null;        try{            //连接到主机              conn.connect();            //使用用户名和密码校验              booleanisconn = conn.authenticateWithPassword(username, password);            if(!isconn)             {                System.out.println("用户名称或者是密码不正确");                        else            {                System.out.println("已经连接OK");                ssh = conn.openSession();                                 ssh.execCommand("sh /root/");                //ssh.execCommand("perl /root/");                  //只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,                //多次使用则会出现异常                       //使用多个命令用分号隔开                  //ssh.execCommand("cd /root; sh");                                 //将Terminal屏幕上的文字全部打印出来                                  InputStream is = newStreamGobbler(ssh.getStdout());                BufferedReader brs = newBufferedReader(newInputStreamReader(is));                while(true                {                    String line = brs.readLine();                    if(line == null                    {                        break;                    }                    System.out.println(line);                }            }                    catch(IOException e)         {            e.printStackTrace();        finally        {            //连接的Session和Connection对象都需要关闭              ssh.close();            conn.close();        }     }}

Java way to remotely connect computers and execute Linux commands via SSH2 (GO)

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: 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.