栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java springboot 执行shell命令

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java springboot 执行shell命令

服务 POM添加:

        
        
            ch.ethz.ganymed
            ganymed-ssh2
            262
        

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/749391.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号