Ganymed远程连接SSH

一、依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>kafkademo</groupId>
    <artifactId>kafkademo</artifactId>
    <version>1</version>
    <dependencies>
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

二、Host类

/**
 * 目标主机
 **/
public class DestHost {
    private String host;
    private String username;
    private String password;
    private int port = 22;
    private int timeout = 60 * 60 * 1000;

    public DestHost(String host, String username, String password) {
        this(host, username, password, 22, 60 * 60 * 1000);
    }

    public DestHost(String host, String username, String password, int timeout) {
        this(host, username, password, 22, timeout);
    }

    public DestHost(String host, String username, String password, int port, int timeout) {
        super();
        this.host = host;
        this.username = username;
        this.password = password;
        this.port = port;
        this.timeout = timeout;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
}

三、GanymedUtils

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

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

/**
 * ganym连接linux
 **/
public class GanymedUtils {
    private static final int TIME_OUT = 0;
    public static Session getSession(DestHost host) {
        Connection connection = new Connection(host.getHost());
        try {
            connection.connect();
            boolean isLogin = connection.authenticateWithPassword(host.getUsername(), host.getPassword());
            if (!isLogin) {
                throw new Exception("远程登录服务器失败");
            }
            Session session = connection.openSession();
            // 建立虚拟终端
            session.requestPTY("bash");
            // 打开一个Shell
            session.startShell();
            return session;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    public static String executeShell(DestHost host, String[] commands) {
        Session session = getSession(host);
        PrintWriter out = new PrintWriter(session.getStdin());
        for (String command : commands) {
            out.println(command);
        }
        out.flush();
        out.close();
        try {
            session.waitForCondition(ChannelCondition.CLOSED | ChannelCondition.EOF |
                    ChannelCondition.EXIT_STATUS , TIME_OUT);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String result = getResult(new StreamGobbler(session.getStdout()));
        session.close();
        return result;
    }
    private static String getResult(InputStream in) {
        byte[] buf = new byte[1024];
        StringBuilder sb = new StringBuilder();
        try {
            while (in.read(buf) != -1) {
                String t = new String(buf, StandardCharsets.UTF_8);
                System.out.print(t);
                sb.append(t);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        DestHost host = new DestHost("192.168.163.130", "oracle", "xxxxxx");
        String[] commands = new String[]{
                "cd /home/oracle/",
                "pwd",
                "exit"
        };
        String result = GanymedUtils.executeShell(host, commands);
        System.out.println(result);
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值