服务 POM添加:

        <!-- ssh连接执行Shell-->
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
        </dependency>

1.获取一个shh连接会话(关键内容)

package com.wang.systemrelease.utils;

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

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

public class RemoteShellTool {
    private Connection conn;
    private String ipAddr;
    private String charset = Charset.defaultCharset().toString();
    private String userName;
    private String password;

    public RemoteShellTool(String ipAddr, String userName, String password,
                           String charset) {
        this.ipAddr = ipAddr;
        this.userName = userName;
        this.password = password;
        if (charset != null) {
            this.charset = charset;
        }
    }

    public boolean login() throws IOException {
        conn = new Connection(ipAddr); //默认端口22
        conn.connect(); // 连接
        return conn.authenticateWithPassword(userName, password); // 认证
    }

    public void close() {
        conn.close();
    }

    public void execShell(String cmds) {
        try {
                Session session = conn.openSession(); // 打开一个会话
                session.requestDumbPTY();
                session.startShell();
                PrintWriter pw = new PrintWriter(session.getStdin());
                pw.println(cmds);
                pw.close();
                session.close();

        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

    //获取查询结果
    public String exec(String cmds) {
        InputStream in = null;
        String result = "";
        try {
            Session session = conn.openSession(); // 打开一个会话
            session.execCommand(cmds);
            in = session.getStdout();
            result = this.processStdout(in, this.charset);
            in.close();
            session.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return result;
    }


    public String processStdout(InputStream in, String charset) {
        byte[] buf = new byte[1024];
        StringBuffer sb = new StringBuffer();
        try {
            while (in.read(buf) != -1) {
                sb.append(new String(buf, charset));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }


}

2.调用会话执行

    /**
    * 连接服务器发布命令
    */
    public R start(ReleaseParam param,HostParam host) {
        RemoteShellTool tool = new RemoteShellTool(host.getHostName(), host.getUsername(),
                host.getPassword(), "utf-8");
        try {
            if(!tool.login()){
                return new R(-1, "服务器连接失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /**
        * 服务是否已经停止
        */
        String name = param.getEdition();
        ReleaserRecord oldRecord = mapper.getByPublisId(param.getPublishConfigurationId());
        String oldname = name;
        if(oldRecord != null){
           oldname = oldRecord.getEdition();
        }

        boolean isStart = close(tool, 0,oldname);
        if (isStart) {
            tool.execShell(param.getPreCommand()+" "+host.getTargetPath()+ name + " " +param.getPostCommand());
            boolean start = isStart(tool, 0,name);
            if(!start){
                return new R(-1, "服务启动超时");
            }
        } else {
            return new R(-1, "服务停止超时");
        }
        tool.close();
        return new R();
    }

Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐