java_ssh_linux

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);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值