百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

Java ssh实现远程服务器部署(java远程登录服务器)

toqiye 2024-09-04 20:06 13 浏览 0 评论

pom依赖

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


BtpAbstractService类

/**
 * <p>Title: abstract service</p>
 * <p>Author: lehoon</p>
 * <p>Date: 2022/2/28 16:48</p>
 */
@Slf4j
public abstract class BtpAbstractService {
    protected String deployPath;
    protected String deployBasePath;
    //service operate request
    protected BtpDeployServiceBaseEntity serviceBaseEntity;

    public BtpAbstractService(BtpDeployServiceBaseEntity serviceBaseEntity) {
        this.serviceBaseEntity = serviceBaseEntity;
    }

    protected boolean isDeployBasePathEnvEmpty() {
        deployBasePath = StringUtils.trimToEmpty(getDeployBasePath());
        return StringUtils.isBlank(deployBasePath);
    }

    protected String getDeployBasePath() {
        return getEnvByName(BtpDeployNodeEnvType.BTP_DEPLOY_NODE_ENV_TYPE_DEPLOY_HOMEPATH.getValue());
    }

    protected String getEnvByName(String name) {
        if (serviceBaseEntity == null || CollectionUtil.isEmpty(serviceBaseEntity.getEnvMap())) return null;
        if (serviceBaseEntity.getEnvMap().containsKey(name)) return serviceBaseEntity.getEnvMap().get(name).getEnvValue();
        return null;
    }

    protected void updateDeployPath() {
        deployPath = String.format("%s/%s", fixFsFilePath(deployBasePath), serviceBaseEntity.getServiceId());
    }

    protected boolean checkNodeNetState() {
        return TcpHelper.isRemoteAlive(serviceBaseEntity.getNodeHost(), serviceBaseEntity.getNodePort());
    }

    protected String shellDisableEchoCmd() {
        return " >> /dev/null 2>&1";
    }

    protected void sleepCmd() {
        sleep(500);
    }

    protected void sleep3() {
        sleep(2000);
    }

    protected void sleep5() {
        sleep(5000);
    }

    protected void sleep(int timeout) {
        try {
            Thread.sleep(timeout);
        } catch (InterruptedException e) {
        }
    }

    protected String fixFsFilePath(String path) {
        if (path == null || path.length() == 0) return "";
        if (!path.endsWith("/")) return path;
        return path.substring(0, path.length() - 1);
    }

    protected boolean isDeployBasePathValid() {
        return deployBasePath != null && deployBasePath.startsWith("/");
    }


    abstract protected void preCheck() throws BtpDeployException;
    abstract protected void nextCheck() throws BtpDeployException;
    abstract protected void process() throws BtpDeployException;
    abstract protected void rollBack() throws BtpDeployException;
}


BtpAbstractSSHService类

/**
 * <p>Title: ssh 操作</p>
 * <p>Description: </p>
 * <p>Author: lehoon</p>
 * <p>Date: 2022/1/20 15:38</p>
 */
@Slf4j
public abstract class BtpAbstractSSHService extends BtpAbstractService {
    //ssh connection
    protected Connection connection = null;

    public BtpAbstractSSHService(BtpDeployServiceBaseEntity serviceBaseEntity) {
        super(serviceBaseEntity);
    }

    public void service() throws BtpDeployException {
        preCheck();

        try {
            login(1000);
        } catch (BtpDeployException e) {
            log.error("登陆远程服务器失败. ", e);
            throw e;
        }

        try {
            nextCheck();
            sleep3();
            process();
            disConnect();
        } catch (BtpDeployException e) {
            log.error("部署服务操作失败, 错误信息,", e);
            rollBack();
            disConnect();
            throw e;
        } finally {
            disConnect();
        }
    }

    public void disConnect() {
        if (connection == null) return;
        connection.close();
        connection = null;
    }

    /**
     * 登陆远程服务器
     * @param timeout
     * @throws Exception
     */
    public void login(int timeout) throws BtpDeployException {
        connection = new Connection(serviceBaseEntity.getNodeHost(), serviceBaseEntity.getNodePort());
        try {
            connection.connect(null, timeout, 0);
            boolean isAuthenticate = connection.authenticateWithPassword(serviceBaseEntity.getUserName(), serviceBaseEntity.getPassword());
            if (!isAuthenticate) {
                throw new BtpDeployException("用户名密码错误,登陆失败");
            }
        } catch (IOException e) {
            log.error("登陆远程服务器失败,", e);
            if (e.getCause().getMessage().indexOf("method password not supported") != -1) {
                throw new BtpDeployException("远程服务器不支持密码认证, 请修改ssh配置文件");
            }

            throw new BtpDeployException("登陆远程服务器失败, 请检查原因");
        } catch (Exception e){
            throw new BtpDeployException("登陆远程服务器失败, 请检查原因");
        }
    }

    private void checkConnectState() throws IOException {
        if (connection == null) throw new IOException("未与服务器建立连接, 不能执行命令.");
        if (!connection.isAuthenticationComplete()) throw new IOException("与服务器连接认证未通过, 不能执行命令.");
    }

    private String readCmdResult(InputStream inputStream) {
        StringBuilder result = new StringBuilder(1024);

        try {
            InputStream stdout = new StreamGobbler(inputStream);
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            String cmdResLine = null;

            while (true) {
                cmdResLine = br.readLine();
                if (cmdResLine == null) {
                    break;
                }
                result.append(cmdResLine);
            }
        } catch (IOException e) {
            log.error("读取远程服务器shell指令结果失败, ", e);
        }

        return result.toString();
    }

    public boolean executeCmd(String cmd) throws BtpDeploySSHExecException {
        try {
            return execute1(cmd);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BtpDeploySSHExecException(e.getMessage());
        }
    }

    private Session openSession() throws Exception {
        checkConnectState();

        try {
            return connection.openSession();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new Exception("打开终端执行环境会话失败,执行命令失败.");
        }
    }

    /**
     * 执行命令
     * @param cmd
     * @return
     * @throws IOException
     */
    public String execute(String cmd) throws Exception {
        sleepCmd();
        Session session = openSession();
        preHandleSession(session);
        session.execCommand(cmd);
        String result = readCmdResult(session.getStdout());
        log.info("执行命令execute[{}],返回结果为[{}]",  cmd, result);
        session.close();
        return result;
    }

    public boolean execute1(String cmd) throws Exception {
        sleepCmd();
        Session session = openSession();
        preHandleSession(session);
        session.execCommand(cmd);
        String result = readCmdResult(session.getStdout());
        log.info("执行命令execute1[{}],返回结果为[{}]", cmd, result);
        int code = 0;
        try {
            code = session.getExitStatus();
            log.info("执行命令execute1[{}],操作码为[{}],返回结果为[{}]", cmd, code, result);
        } catch (Exception e) {
            log.error("执行命令出错", e.fillInStackTrace());
        } finally {
            if (session != null) session.close();
        }
        return code == 0;
    }

    /**
     * 创建目录
     * @param dir
     * @return
     */
    public String mkdir(String dir) throws BtpDeploySSHExecException {
        String cmd = String.format("mkdir -p %s", dir);
        try {
            return execute(cmd);
        } catch (Exception e) {
            log.error("创建目录失败,", e);
            throw new BtpDeploySSHExecException(e.getMessage());
        }
    }

    public boolean mkdir1(String dir) throws BtpDeploySSHExecException {
        String cmd = String.format("mkdir -p %s", dir);
        return executeCmd(cmd);
    }

    public boolean createDirectory(String directory) throws BtpDeploySSHExecException{
        return mkdir1(directory);
    }

    /**
     * 解压zip文件到指定目录
     * @param zipSrc
     * @param distDir
     * @return
     */
    public boolean unzip(String zipSrc, String distDir) throws BtpDeploySSHExecException {
        String cmd = String.format("unzip -oq %s -d %s", zipSrc, distDir);
        return executeCmd(cmd);
    }

    /**
     * 修改文件内容
     * @param src
     * @param dist
     * @param filePath
     * @return
     * @throws Exception
     */
    public String replaceInFile(String src, String dist, String filePath) throws BtpDeploySSHExecException {
        String cmd = String.format("sed -i 's/%s/%s/' %s", src, dist, filePath);
        try {
            return execute(cmd);
        } catch (Exception e) {
            log.error("修改文件内容失败", e);
            throw new BtpDeploySSHExecException();
        }
    }

    public boolean rmdir(String filePath) throws BtpDeploySSHExecException {
        if (filePath == null || filePath.length() == 0) return false;
        for (String path : SAFE_DELETE_FILESYSTEM_PATH_DEFAULT) {
            if (path.equalsIgnoreCase(filePath)) throw new BtpDeploySSHExecException(String.format("目录[%s]不允许删除", filePath));
        }

        String cmd = String.format("rm -rf %s", filePath);
        return executeCmd(cmd);
    }

    protected boolean deleteDirectory(String directory) throws BtpDeploySSHExecException {
        if (directory == null || directory.length() == 0 || "/".equalsIgnoreCase(directory)) return true;
        String cmd = String.format("rm -rf %s", directory);
        return executeCmd(cmd);
    }

    /**
     * 删除文件
     * @return
     * @throws IOException
     */
    protected boolean deleteFile(String filePath) throws BtpDeploySSHExecException {
        if (filePath == null || filePath.length() == 0 || "/".equalsIgnoreCase(filePath)) return true;
        String cmd = String.format("rm -rf %s", filePath);
        return executeCmd(cmd);
    }

    protected boolean deleteFile1(String filePath) {
        try {
            return deleteFile(filePath);
        } catch (BtpDeploySSHExecException e) {
            return false;
        }
    }

    /**
     * 文件拷贝:war包+策略zip文件
     * src=路径+文件名;des=目的路径
     *
     * */
    public void copyDoucment(String src, String des)
            throws BtpDeploySSHExecException {
        if (connection == null) throw new BtpDeploySSHExecException("未与服务器建立连接, 不能执行上传文件命令.");
        try {
            SCPClient scpClient = connection.createSCPClient();
            scpClient.put(src, des);
        } catch (IOException e) {
            log.error("上传文件到远程服务器失败,", e);
            throw new BtpDeploySSHExecException();
        }
    }

    /**
     * 进入指定目录并返回目录名称
     * @param basePath
     * @return
     * @throws IOException
     */
    public String cmdPwd(String basePath) throws BtpDeploySSHExecException {
        String cmd = String.format("cd %s && pwd", basePath);
        try {
            return execute(cmd);
        } catch (Exception e) {
            log.error(String.format("切换文件目录失败,目录[%s]不存在", basePath), e);
            throw new BtpDeploySSHExecException();
        }
    }

    public String moveFile(String oldPath, String newPath) throws BtpDeploySSHExecException {
        String cmd = String.format("mv %s %s", oldPath, newPath);
        try {
            return execute(cmd);
        } catch (Exception e) {
            log.error(String.format("移动文件失败,原文件[%s]目标目录[%s]不存在", oldPath, newPath), e);
            throw new BtpDeploySSHExecException();
        }
    }

    public String lsCmd(String filePath) throws BtpDeploySSHExecException {
        String cmd = String.format("ls %s", filePath);
        try {
            return execute(cmd);
        } catch (Exception e) {
            log.error(String.format("获取文件路径失败,文件[%s]不存在", filePath), e);
            throw new BtpDeploySSHExecException();
        }
    }

    public boolean checkFileExist(String filePath) throws BtpDeploySSHExecException {
        String checkFilePath = lsCmd(filePath);

        if (filePath.equalsIgnoreCase(checkFilePath)) {
            return true;
        }

        return false;
    }


    /**
     * 上传文件到远程服务器
     * @param localFileName
     * @param remotePath
     * @param remoteFileName
     * @throws IOException
     */
    public void uploadFileToRemote(String localFileName, String remotePath, String remoteFileName)
            throws BtpDeploySSHExecException {
        if (connection == null) throw new BtpDeploySSHExecException("未与服务器建立连接, 不能执行上传文件命令.");

        try {
            SCPClient scpClient = connection.createSCPClient();// 建立SCP客户端:就是为了安全远程传输文件
            scpClient.put(localFileName, remoteFileName, remotePath, "0600");
        } catch (IOException e) {
            log.error("上传文件到远程服务器失败,", e);
            throw new BtpDeploySSHExecException();
        }
    }

    public boolean uploadFile2Remote(String localFileName, String remotePath, String remoteFileName) {
        try {
            uploadFileToRemote(localFileName, remotePath, remoteFileName);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    protected boolean checkDeployBasePathExist() throws BtpDeploySSHExecException {
        String tempPathPwd = cmdPwd(deployBasePath);

        if (StringUtils.isBlank(tempPathPwd)) {
            log.info("远程服务器上部署根目录在远程服务器上不存在, {}", deployBasePath);
            return false;
        }

        deployBasePath = tempPathPwd;
        return true;
    }

    protected boolean checkDeployPathExist() throws BtpDeploySSHExecException {
        String tempPathPwd = cmdPwd(deployPath);

        if (StringUtils.isBlank(tempPathPwd)) {
            log.info("远程服务器上部署目录在远程服务器上不存在{}", deployPath);
            return false;
        }

        deployPath = tempPathPwd;
        return true;
    }

    protected boolean dos2unix(String filePath) throws BtpDeploySSHExecException {
        if (filePath == null || filePath.length() == 0) return false;
        String cmd = String.format("dos2unix %s", filePath);
        return executeCmd(cmd);
    }

    protected String getFileName(String filename) {
        if (filename == null || filename.length() == 0) return null;
        return filename.substring(0, filename.lastIndexOf("."));
    }

    private void preHandleSession(Session session) throws IOException {
        session.requestPTY("vt100");
    }

    private static List<String> SAFE_DELETE_FILESYSTEM_PATH_DEFAULT
            = new ArrayList<String>(16);

    static {
        SAFE_DELETE_FILESYSTEM_PATH_DEFAULT.add("/");
        SAFE_DELETE_FILESYSTEM_PATH_DEFAULT.add("/boot");
        SAFE_DELETE_FILESYSTEM_PATH_DEFAULT.add("/home");
        SAFE_DELETE_FILESYSTEM_PATH_DEFAULT.add("/opt");
        SAFE_DELETE_FILESYSTEM_PATH_DEFAULT.add("/usr");
        SAFE_DELETE_FILESYSTEM_PATH_DEFAULT.add("/etc");
    }
}


具体操作类通过实现BtpAbastractSSHApamaService类即可

相关推荐

在Vue中使用JSX,很easy的

?此账号为华为云开发者社区官方运营账号,提供全面深入的云计算前景分析、丰富的技术干货、程序样例,分享华为云前沿资讯动态本文分享自华为云社区《在Vue中如何使用JSX,就这么简单!【建议收藏】》,...

深度学习走进死胡同了?

人工智能真正的前路究竟在何方?今天的话题很大,咱们先从浅显的环节入手。深度学习“教父”、在世科学家中的翘楚GeoffreyHinton曾在2016年多伦多召开的一场AI大会上坦言,“放射...

“信息引导”超全总结,让你的设计有理有据

讲信息引导方式的有很多,而本文主要从“是否会打扰用户”的维度,分两个类型举例分析了其中的设计以及原理。上一篇讲了信息引导的策略层:可切入的场景和机制《系统性地教你:如何设计产品的信息引导?》。这篇就总...

Blazor 全屏按钮/全屏服务 (BootstrapBlazor组件库)

Blazor简介Blazor是一个使用.NET生成的交互式客户端WebUI的框架。和前端同学所熟知的Vue、React、Angular有巨大差异。其最大的特色是使用C#代码(理论...

一起学Vue:UI框架(element-ui)

目标使用Vue+ElementUI构建一个非常简单CRUD应用程序,以便您更好地了解它的工作方式。效果页面比如我们要实现这样列表、新增、编辑三个页面:列表页面新增页面编辑页面安装element我们使用...

在Vue.js + Element UI的表格中优雅地实现图片放大功能

引言在Web应用中,表格常常用于展示数据集,而图片则是数据可视化的重要组成部分。为了提升用户体验,我们通常需要允许用户在不跳转页面的情况下,直接从表格中查看图片的原始尺寸。本文将引导你通过Vue.js...

Element-ui简单使用

什么是Element-ui?根据官网的说法,Element-ui,是一套为开发者、设计师和产品经理准备的基于Vue2.0的由饿了么公司出品的桌面端组件库。官网:https://element.ele...

极致舒适的Vue弹窗使用方案

一个Hook让你体验极致舒适的Dialog使用方式!Dialog地狱为啥是地狱?因为凡是有Dialog出现的页面,其代码绝对优雅不起来!因为一旦你在也个组件中引入Dialog,就最少需要额外维护一个v...

VUE3+ts开发弹出框插件

1.插件页面代码取消确认˂...

十分钟,让你学会Vue的这些巧妙冷技巧

前言写了两年的Vue,期间学习到好几个提高开发效率和性能的技巧,现在把这些技巧用文章的形式总结下来。1.巧用$attrs和$listeners$attrs用于记录从父组件传入子组件的所有不被prop...

element的嵌套dialog,在打开第二个dialog的时候会被遮罩层遮住

在一个弹出层的内容区做处理打开另一个弹出层时,第二个弹出层会被遮罩层遮住。对于确实需要嵌套Dialog的场景,提供了append-to-body属性。将内层Dialog的该属性设置为true...

无代码平台之自定义组件

无代码平台就是一个一个的组件当成积木,通过拖拉拽的方式搭建起来,构建自己的应用。前端有前端组件,如果不够自己增加组件,后端有后端的组件,如果不够自己增加组件,用统一的组件间传参、组件穿透、前后端穿透等...

Tailwindcss 入门

是什么?Tailwindcss是一个功能类优先的CSS框架,通过flex,pt-4,text-center和rotate-90这种原子类组合快速构建网站,而不需要离开你的HTML。...

推荐一款比flex更强大的CSS布局——Grid布局

flex布局是一维布局Grid布局是二维布局flex布局一次只能处理一个维度上的元素布局,一行或者一列Grid布局是将容器划分成了“行”和“列”,产生了一个个的网格。Grid基础display...

前端入门——css 网格轨道详细介绍

上篇前端入门——cssGrid网格基础知识整体大概介绍了cssgrid的基本概念及使用方法,本文将介绍创建网格容器时会发生什么?以及在网格容器上使用行、列属性如何定位元素。在本文中,将介绍:...

取消回复欢迎 发表评论: