spring boot远程SSH执行linuxshell命令

1、安装依赖

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

2、编写工具类

package com.example.springboot3.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();
    }
}

3、执行工具类

package com.example.springboot3.controller;

import com.example.springboot3.utils.RemoteShellTool;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SshController {

    @GetMapping("/ssh")
    public String start() {
        RemoteShellTool tool = new RemoteShellTool("localhost", "xxxxx",
                "xxxxxx", "utf-8");
        try {
            if(!tool.login()){
                return "服务器连接失败";
            }
        } 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, "服务停止超时");
//        }
        String result=tool.exec("df -h");
        tool.close();
        System.out.println("计算结果");
        System.out.println(result);
        return result;
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot项目中,我们可以使用bash脚本来启动Linux服务器上的应用程序。 首先,我们需要编写一个启动脚本,可以创建一个以.sh为后缀的文件,例如start.sh。然后,我们可以使用vi或其他文本编辑器打开脚本文件。 在脚本文件中,我们需要配置一些环境变量和启动参数。首先,我们需要设置JAVA_HOME,表示Java的安装目录,例如: ``` export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64 ``` 接下来,我们需要设置应用程序的一些参数,比如应用程序的JAR包路径、日志文件路径等。在Spring Boot项目中,我们可以使用以下命令来启动项目: ``` java -jar /path/to/your/springbootapp.jar ``` 请将"/path/to/your/springbootapp.jar"替换为实际的JAR包路径。 最后,我们可以使用nohup命令将应用程序以后台模式启动,并将输出信息重定向到一个日志文件中。例如: ``` nohup java -jar /path/to/your/springbootapp.jar > /path/to/your/log/file.log & ``` 请将"/path/to/your/log/file.log"替换为实际的日志文件路径。 保存并退出脚本文件。然后,我们需要给脚本文件添加可执行权限,使用以下命令: ``` chmod +x start.sh ``` 现在,我们可以运行启动脚本来启动Spring Boot应用程序了: ``` ./start.sh ``` 脚本会自动配置所需的环境变量和启动参数,并将应用程序以后台模式启动。你可以通过tail命令查看日志文件来确认应用程序是否成功启动。例如: ``` tail -f /path/to/your/log/file.log ``` 以上就是在Linux服务器上使用启动脚本启动Spring Boot项目的简单步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员阿明

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值