小言_互联网的博客

java_ssh_linux

272人阅读  评论(0)

pom.xml

<dependency>
			<groupId>ch.ethz.ganymed</groupId>
			<artifactId>ganymed-ssh2</artifactId>
			<version>build210</version>
</dependency>

RemoteSSHUtil的工具类使用


端口默认的使用了22 如果想使用其它端口得自己改一下

package com.qbsea.utils;

import ch.ethz.ssh2.ChannelCondition;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class  RemoteSSHUtil {

    public static String execCommand(String hostName,String userName,String passWord,String command) throws Exception {

        final int TIME_OUT = 1000*5*60;
        Session session = null;
        StringBuilder sb = new StringBuilder();
        Connection conn = new Connection(hostName, 22);
        try {
            conn.connect(null, 5000, 0);
            boolean isAuthenticated = conn.authenticateWithPassword(userName, passWord);
            if (!isAuthenticated) {
                throw new IOException("Authentication failed.");
            }
            session = conn.openSession();
            session.execCommand(command);
            InputStream stdout = session.getStdout();
            InputStream stderr = session.getStderr();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String s;
                    try (BufferedReader br = new BufferedReader(new InputStreamReader(stdout))) {
                        while ((s = br.readLine()) != null) {
                            sb.append(s).append("\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String s;
                    try (BufferedReader br = new BufferedReader(new InputStreamReader(stderr))) {
                        while ((s = br.readLine()) != null) {
                            sb.append(s).append("\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
            session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
            if (sb.indexOf("\n") == -1) {
                return sb.toString();
            }
            return sb.toString().substring(0, sb.lastIndexOf("\n"));
        } catch (Exception e) {
            throw new Exception("this command execute error");
        }finally {
            if (session != null) {
                session.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }

    public static void main(String[] args) throws Exception {
        String execCommand = execCommand("127.0.0.1",
                                "root",
                                "password",
                                "cd /root ; touch 1.txt");
        System.out.println("execCommand="+execCommand);
    }

}


转载:https://blog.csdn.net/maqingbin8888/article/details/101313447
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场