java 连接SSH工具操作服务器 (构建者模式+Util类) 分享

需求

因为需要以java 远程操作服务器, 比如进行文件下载/上传操作, 或者执行一些服务器常用命令ls cat grep 等等. 调研发现比较好用的SSH 工具有:

  • ganymed-ssh2
  • jsch
  • sshj 等.
    这些工具最主要的区别在于SSH加密算法的丰富性
    ganymed-ssh2 < jsch< sshj

因为目前服务器的安全性上 ganymed-ssh2 可以满足,所以本工具基于ganymed-ssh2 封装

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

静态方法工具类

优点:
直接使用SSHUtil.xxx方法完成部分操作,简洁无脑
缺点:
方法为单独调用, 操作不方便 (因为有时候需要使用多个步骤)

使用方式 : 基本顺序式调用

Connection connection= SSHUtil.getConnect(“”,“”,“”)
SFTPv3Client sftpClient = new SFTPv3Client(connection);
// 查看删除等操作
sftpClient.rm(“”);
sftpClient.ls(“”);
sftpClient.mv(“”);
// 或者执行命令行
Session sess = connection.openSession();
sess.execCommand("mv a b);
sess.close();
connection.close();

import ch.ethz.ssh2.*;
import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * @author jee
 * @version 1.0
 */
public class SSHUtil {

    private static final Logger logger = LoggerFactory.getLogger(SSHUtil.class);


    /**
     * 使用默认的22端口
     * 获取ssh服务连接
     *
     * @param serverIp 服务器IP
     * @param username 用户名
     * @param password 密码
     * @return ssh连接对象
     * @throws SSHException ssh连接异常
     */
    public static Connection getConnect(String serverIp, String username, String password) throws SSHException {
        return getConnect(serverIp, 22, username, password);
    }

    /**
     * 获取ssh服务连接
     *
     * @param serverIp 服务器IP
     * @param port     端口
     * @param username 用户名
     * @param password 密码
     * @return ssh连接对象
     * @throws SSHException ssh连接异常
     */
    public static Connection getConnect(String serverIp, int port, String username, String password) throws SSHException {
        Connection connection = new Connection(serverIp, port);
        try {
            connection.connect();
            connection.authenticateWithPassword(username, password);
            return connection;
        } catch (IOException e) {
            throw new SSHException("服务连接异常", e);
        }
    }

    /**
     * 远程拷贝文件
     *
     * @param remoteFile           远程源文件路径
     * @param localTargetDirectory 本地存放文件路径
     */
    public void downloadFile(Connection connection, String remoteFile, String localTargetDirectory) throws SSHException {
        try {
            SCPClient client = new SCPClient(connection);
            client.get(remoteFile, localTargetDirectory);
            connection.close();
        } catch (IOException e) {
            throw new SSHException("拷贝文件异常!", e);
        }
    }


    /**
     * 远程上传文件
     *
     * @param localFile             本地文件路径
     * @param remoteTargetDirectory 远程存放文件路径
     */
    public void uploadFile(Connection connection, String localFile, String remoteTargetDirectory) throws SSHException {
        try {
            SCPClient client = new SCPClient(connection);
            client.put(localFile, remoteTargetDirectory);
            connection.close();
        } catch (IOException e) {
            throw new SSHException("上传文件异常!", e);
        }
    }

    /**
     * 远程上传文件 并指定上传文件名称
     *
     * @param localFile             本地文件路径
     * @param remoteFileName        远程文件名
     * @param remoteTargetDirectory 远程存放文件路径
     * @param mode                  默认"0600",length=4
     */
    public static void uploadFile(Connection connection, String localFile, String remoteFileName, String remoteTargetDirectory, String mode) throws SSHException {
        try {
            SCPClient client = new SCPClient(connection);
            if (StrUtil.isBlank(mode)) {
                mode = "0600";
            }
            client.put(localFile, remoteFileName, remoteTargetDirectory, mode);
            //重命名
            String tmpPathName = remoteTargetDirectory + File.separator + remoteFileName;
            String newPathName = tmpPathName.substring(0, tmpPathName.lastIndexOf("."));
            String cmd = StrUtil.format("mv {} {}", remoteFileName, newPathName);
            execShell(connection, cmd);
            connection.close();
        } catch (IOException e) {
            throw new SSHException("上传文件异常!", e);
        }
    }


    /**
     * 执行shell命令
     *
     * @param connection 服务器连接对象
     * @param cmd        shell命令
     * @throws SSHException 执行shell异常
     */
    public static void execShell(Connection connection, String cmd) throws SSHException {
        //重命名
        try {
            Session session = connection.openSession();
            session.execCommand(cmd);
            session.close();
        } catch (IOException e) {
            throw new SSHException("执行shell异常!", e);
        }
    }
}

使用构造者模式升级工具

基于Consumer 加builder模式进行构建

import ch.ethz.ssh2.*;
import cn.hutool.core.io.IoUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

/**
 * SSH 构建者模式
 *
 * @author jee
 * @version 1.0
 */
public class SSHBuilder {
    /**
     * 连接对象
     */
    private Connection connection;
    /**
     * session会话
     */
    private Session session;


    public SSHBuilder conn(String ip, int port, String username, String password) throws SSHException {
        if (connection == null) {
            connection = SSHUtil.getConnect(ip, port, username, password);
        }
        return this;
    }
    public SSHBuilder conn(String ip, String username, String password) throws SSHException {
        conn(ip, 22, username, password);
        return this;
    }

    /**
     * scp 操作
     *
     * @param consumer 提供SCPClient对象
     * @return ssh对象
     * @throws SSHException ssh操作异常
     */
    public SSHBuilder scp(Consumer<SCPClient> consumer) throws SSHException {
        if (connection == null) {
            throw new IllegalArgumentException("未建立连接");
        }
        consumer.accept(new SCPClient(connection));
        return this;
    }

    /**
     * sftp操作
     *
     * @param consumer 提供SFTPv3Client 操作对象
     * @return ssh对象
     * @throws SSHException ssh操作异常
     */
    public SSHBuilder sftp(Consumer<SFTPv3Client> consumer) throws SSHException {
        if (connection == null) {
            throw new IllegalArgumentException("未建立连接");
        }
        try {
            consumer.accept(new SFTPv3Client(connection));
        } catch (IOException e) {
            throw new SSHException("构建sftp异常", e);
        }
        return this;
    }


    /**
     * 无返回结果的命令行操作
     *
     * @param cmd shell命令
     * @return ssh对象
     * @throws SSHException ssh操作异常
     */
    public SSHBuilder exec(String cmd) throws SSHException {
        return exec(cmd, null);
    }


    /**
     * 命令行操作
     *
     * @param cmd      shell命令
     * @param consumer 提供执行结果行集合文本
     * @return ssh对象
     * @throws SSHException ssh操作异常
     */
    public SSHBuilder exec(String cmd, Consumer<List<String>> consumer) throws SSHException {
        if (connection == null) {
            throw new IllegalArgumentException("未建立连接");
        }

        try {
            session = connection.openSession();
            session.execCommand(cmd);
            if (consumer != null) {
                List<String> lines = IoUtil.readUtf8Lines(session.getStdout(), new ArrayList<>());
                //读取错误信息
                if (lines.size() == 0) {
                    lines = IoUtil.readUtf8Lines(session.getStderr(), new ArrayList<>());
                }
                consumer.accept(lines);
            }
        } catch (IOException e) {
            throw new SSHException("构建exec异常", e);
        }
        return this;
    }

    public void close() {
        if (session != null) {
            session.close();
        }

        if (connection != null) {
            connection.close();
        }

    }
}

使用方式

1. scp操作

   // 上传文件
        new SSHBuilder().conn("xxx.xxx.xxx.xxx", "root", "xxxx").scp(scp -> {
            try {
                scp.get("", "");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).close();

2.sftp操作

 // 删除文件
        new SSHBuilder().conn("xxx.xxx.xxx.xxx", "root", "xxxx").sftp(sftp -> {
            try {
                sftp.rm("");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).close();

3. 链式多操作

    // sftp+ 命令 操作
        new SSHBuilder().conn("xxx.xxx.xxx.xxx", "root", "xxxx")
                .sftp(sftp -> {
                    try {
                        sftp.mkdir("/test-ssh", 0777);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                })
                .exec("cd /&&ls", lines -> {
                    lines.forEach(s -> {
                        System.out.println(s);
                    });
                }).close();

测试结果如图:

在这里插入图片描述

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值