Java 使用JSch调用ssh或sftp命令

主要功能是在ssh终端上调用程序生成的shell脚本执行想执行的命令。
使用fullWidthToHalfWidth方法是因为程序在获取到请求后传入的参数中,有部分符号会转成全角字符,在shell里会被当做运算符,导致shell脚本报错(参数过多)。
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.jcraft.jsch.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.springframework.scheduling.annotation.Scheduled;

@Component
@EnableScheduling
@Controller
@RequestMapping("/sendCommControllertest")
@Api(value = "SendCommController", tags = "999.调用控制台")
@EnableKnife4j
public class RemoteCommandExecutor {
    private static final String host = "10.1.60.162";
    private static final String user = "root";
    private static final String password = "HT@123456";
    private static final int port = 49168;
    private static final String remoteScriptPath = "/script.sh";
  //  @Scheduled(cron = "0 0 0 * * *") // 每天的00:00:00执行一次
    @PostMapping("/sendComm")
    @ApiOperation(value = "9999、调用控制台", notes = "调用控制台")
    @ResponseBody
    public static void main(@RequestParam(name = "commd") String commd) {
        Timer timer = new Timer();
        // 定义定时任务
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                System.out.println(commd);
                String converted = fullWidthToHalfWidth(commd);
                System.out.println(converted);
                ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
                // 将原来的代码放在这里
                try {
                    JSch jsch = new JSch();
                    Session session = jsch.getSession(user, host, port);
                    session.setPassword(password);

                    // 设置配置
                    java.util.Properties config = new java.util.Properties();
                    config.put("StrictHostKeyChecking", "no");
                    session.setConfig(config);

                    // 连接
                    session.connect();

                    // 打开SFTP通道
                    ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
                    sftpChannel.connect();

                    // 写入脚本内容

                    String scriptContent = "#!/bin/bash\n  " + converted + cpcmd + restartcmd; // 示例脚本内容
                    try (ByteArrayInputStream in = new ByteArrayInputStream(scriptContent.getBytes());
                         OutputStream out = sftpChannel.put(remoteScriptPath)) {
                        byte[] buf = new byte[1024];
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                    }

                    // 设置脚本为可执行
                    sftpChannel.chmod(0755, remoteScriptPath);
                    // 打开一个执行命令的通道
                    ChannelExec execChannel = (ChannelExec) session.openChannel("exec");
                    InputStream in = execChannel.getInputStream();
                    execChannel.setCommand("bash " + remoteScriptPath); // 执行脚本
                    execChannel.connect();

                    // 读取命令输出
                    byte[] tmp = new byte[1024];
                    while (true) {
                        while (in.available() > 0) {
                            int i = in.read(tmp, 0, 1024);
                            if (i < 0) break;
                            System.out.print(new String(tmp, 0, i));
                        }
                        if (execChannel.isClosed()) {
                            if (in.available() > 0) continue;
                            System.out.println("exit-status: " + execChannel.getExitStatus());
                            break;
                        }
                    }
                    System.out.println(cpcmd);
                    System.out.println(restartcmd);
                    // 关闭通道和会话
                    execChannel.disconnect();
                    sftpChannel.disconnect();
                    //session.disconnect();

                } catch (JSchException | SftpException | IOException e) {
                    e.printStackTrace();
                }
            }
        };
        // 定义定时任务的执行间隔,单位为毫秒,这里是每12小时执行一次
        long delay = 0;
       // long period = 86400000;
        long period = 43200000;
        timer.schedule(task, delay, period);

    }
    public static String fullWidthToHalfWidth(String str) {  //全角转半角
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c >= 65281 && c <= 65374) { // 全角字符范围
                sb.append((char) (c - 65248)); // 转换为对应的半角字符
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值