Java 操作 Linux 服務(wù)器 上傳文件并執(zhí)行腳本

一、本項(xiàng)目核心目的

(目前支持.sql 和 .py腳本,.java腳本跟.py腳本大同小異,只是命令不同)

  • 1.從A服務(wù)器獲取腳本文件
  • 2.上傳到B服務(wù)器指定文件夾
  • 3.通過命令執(zhí)行上傳后得腳本文件
  • 4.返回執(zhí)行結(jié)果

二、核心jar包

<!--sftp文件上傳-->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
<!--ssh執(zhí)行-->
      <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
        </dependency>

三、Spring Boot 與 nacos 版本不對(duì)應(yīng)時(shí)會(huì)報(bào)錯(cuò),當(dāng)前Spring Boot版本為2.3.9.RELEASE

四、pom.xml 文件

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>262</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.boot</groupId>
            <artifactId>nacos-config-spring-boot-starter</artifactId>
            <version>0.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-commons</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>
        <!--sftp文件上傳-->
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.54</version>
        </dependency>
    </dependencies>

五、RemoteCommandConfig(服務(wù)器調(diào)用)

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

@Slf4j
@Component
public class RemoteCommandConfig {

    /**
     * 只是用來測(cè)試,無其他用處
     */
    public static void main(String[] args) {
        Connection conn = login("這里填服務(wù)器IP地址", "賬號(hào)", "密碼");
        JSONObject execute = execute(conn, "cd /script/ &&mysql -vvv -uadmin -p123456 spring_data < 測(cè)試.sql");
        System.out.println(execute.toString());
    }

    private static String DEFAULTCHART = "UTF-8";

    /**
     * @return 登錄成功返回true,否則返回false
     * @描述 登錄主機(jī)
     */
    public static Connection login(String ip, String username, String password) {
        boolean flg;
        Connection conn = null;
        try {
            conn = new Connection(ip);
            conn.connect();//連接
            flg = conn.authenticateWithPassword(username, password);//認(rèn)證
            if (flg) {
                return conn;
            }
        } catch (IOException e) {
            log.error("腳本執(zhí)行登錄服務(wù)器失敗,error={}" + e.getMessage());
            e.printStackTrace();
        }
        return conn;
    }

    /**
     * @param cmd 即將執(zhí)行的命令
     * @return 命令執(zhí)行完后返回的結(jié)果值
     * @描述 遠(yuǎn)程執(zhí)行shll腳本或者命令
     */
    public static JSONObject execute(Connection conn, String cmd) {
        JSONObject result = new JSONObject();
        result.put("code", 200);
        String str = "";
        try {
            if (conn != null) {
                Session session = conn.openSession();//打開一個(gè)會(huì)話
                session.execCommand(cmd);//執(zhí)行命令
                str = processStdout(session.getStdout(), DEFAULTCHART);
                //如果為得到標(biāo)準(zhǔn)輸出為空,說明腳本執(zhí)行出錯(cuò)了
                if (StringUtils.isBlank(str)) {
                    result.put("code", 400);
                    result.put("msg", "得到標(biāo)準(zhǔn)輸出為空,鏈接conn:" + conn + ",執(zhí)行的命令:" + cmd);
                    str = processStdout(session.getStderr(), DEFAULTCHART);
                } else {
                    result.put("msg", 200);
                    result.put("msg", "執(zhí)行命令成功,鏈接conn:" + conn + ",執(zhí)行的命令:" + cmd);
                }
                conn.close();
                session.close();
            }
        } catch (IOException e) {
            log.info("執(zhí)行命令失敗,鏈接conn:" + conn + ",執(zhí)行的命令:" + cmd + "  error={}" + e.getMessage());
        }
        result.put("data", str);
        return result;
    }

    /**
     * @param in      輸入流對(duì)象
     * @param charset 編碼
     * @return 以純文本的格式返回
     * @描述 解析腳本執(zhí)行返回的結(jié)果集
     */
    private static String processStdout(InputStream in, String charset) {
        InputStream stdout = new StreamGobbler(in);
        StringBuffer buffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
            String line;
            while ((line = br.readLine()) != null) {
                buffer.append(line + "\n");
            }
        } catch (Exception e) {
            log.error("解析腳本出錯(cuò),error={}" + e.getMessage());
            e.printStackTrace();
        }
        return buffer.toString();
    }

六、RestTemplateConfig防止項(xiàng)目因?yàn)镽estTemplate啟動(dòng)失敗

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder){
        return builder.build();
    }
}

七、ScriptDealWithController (方法整合)

import ch.ethz.ssh2.Connection;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.sd.config.RemoteCommandConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.*;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


//nacos實(shí)時(shí)動(dòng)態(tài)參數(shù)獲取
@RefreshScope
@RestController
@RequestMapping("/script")
public class ScriptDealWithController {
    /**
     * 本項(xiàng)目所在服務(wù)器地址
     */
    @NacosValue(value = "${gyyh_login.ip}")
    private String gyyhIP;//登錄獲取ip
    @NacosValue(value = "${gyyh_login.username}")
    private String gyyhUsername;//系統(tǒng)賬號(hào)
    @NacosValue(value = "${gyyh_login.password}")
    private String gyyhPassword;//系統(tǒng)密碼
    /**
     * MySQL服務(wù)器地址
     */
    @NacosValue(value = "${mysql_script.ip}")
    private String myIp;//MySQL所在ip
    @NacosValue(value = "${mysql_script.username}")
    private String myUsername;//MySQL所在ip賬號(hào)
    @NacosValue(value = "${mysql_script.password}")
    private String myPassword;//MySQL所在ip密碼
    @NacosValue(value = "${mysql_script.command}")
    private String myCommand;//MySQL腳本執(zhí)行命令
    @NacosValue(value = "${mysql_script.file_path}")
    private String myFilePath;//文件保存路徑
    /**
     * pythonL服務(wù)器地址
     */
    @NacosValue(value = "${python_script.ip}")
    private String pyIp;//MySQL所在ip
    @NacosValue(value = "${python_script.username}")
    private String pyUsername;//MySQL所在ip賬號(hào)
    @NacosValue(value = "${python_script.password}")
    private String pyPassword;//MySQL所在ip密碼
    @NacosValue(value = "${python_script.command}")
    private String pyCommand;//MySQL腳本執(zhí)行命令
    @NacosValue(value = "${python_script.file_path}")
    private String pyFilePath;//文件保存路徑

    @Autowired
    private RestTemplate restTemplate;

    /**
     * @描述 獲取平臺(tái)登錄token
     * @參數(shù) []
     * @返回值 java.lang.String
     * @創(chuàng)建時(shí)間 2021/7/23
     */
    public String getToken() {
        String url = gyyhIP + "/獲取token地址";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("username", gyyhUsername);
        params.add("password", gyyhPassword);
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        JSONObject body = JSON.parseObject(response.getBody());
        JSONObject data = body.getJSONObject("data");
        if (CollectionUtils.isEmpty(data)) {
            throw new RuntimeException("token獲取失敗");
        }
        return data.getString("token");
    }

    /**
     * @描述 調(diào)用平臺(tái)文件下載接口獲取文件流
     * @參數(shù) [body]
     * @返回值 byte[]
     * @創(chuàng)建時(shí)間 2021/7/23
     */
    public byte[] download(String token, String type, String downloadId) {
        String url = gyyhIP + "文件下載接口地址";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Authorization", token);
        Map<String, String> params = new HashMap<>();
        params.put("type", type);
        params.put("downloadId", downloadId);
        HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(params, headers);
        ResponseEntity<byte[]> response = restTemplate.postForEntity(url, requestEntity, byte[].class);
        return response.getBody();
    }

    /**
     * @描述 寫入文件到指定目錄下
     * @參數(shù) [bfile, filePath, fileName]
     * @返回值 void
     * @創(chuàng)建時(shí)間 2021/7/23
     */
    public void upload(byte[] bfile, String ip, String username, String password, String filePath, String fileName) throws Exception {
        //服務(wù)器端口 默認(rèn)22
        int port = 22;
        Session session = null;
        Channel channel = null;
        JSch jsch = new JSch();
        if (port <= 0) {
            //連接服務(wù)器,采用默認(rèn)端口
            session = jsch.getSession(username, myIp);
        } else {
            //采用指定的端口連接服務(wù)器
            session = jsch.getSession(username, ip, port);
        }
        //如果服務(wù)器連接不上,則拋出異常
        if (session == null) {
            throw new Exception("session is null");
        }
        //設(shè)置登陸主機(jī)的密碼
        session.setPassword(password);//設(shè)置密碼
        //設(shè)置第一次登陸的時(shí)候提示,可選值:(ask | yes | no)
        session.setConfig("StrictHostKeyChecking", "no");
        //設(shè)置登陸超時(shí)時(shí)間
        session.connect(30000);
        OutputStream outstream = null;
        try {
            //創(chuàng)建sftp通信通道
            channel = session.openChannel("sftp");
            channel.connect(1000);
            ChannelSftp sftp = (ChannelSftp) channel;
            //進(jìn)入服務(wù)器指定的文件夾
            sftp.cd(filePath);
            //以下代碼實(shí)現(xiàn)從本地上傳一個(gè)文件到服務(wù)器,如果要實(shí)現(xiàn)下載,對(duì)換以下流就可以了
            outstream = sftp.put(fileName);
            outstream.write(bfile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //關(guān)流操作
            if (outstream != null) {
                outstream.flush();
                outstream.close();
            }
            if (session != null) {
                session.disconnect();
            }
            if (channel != null) {
                channel.disconnect();
            }
        }
    }

    /**
     * @描述 腳本執(zhí)行
     * @參數(shù) [body]
     * @返回值 java.lang.String
     * @創(chuàng)建時(shí)間 2021/7/27
     */
    @PostMapping("/execute")
    public JSONObject execute(@RequestBody JSONObject body) throws Exception {
        String fileName = "";
        String type = body.getString("type");
        String downloadId = body.getString("downloadId");
        //腳本類型 sql 、python
        String languageType = body.getString("languageType");
        Connection conn = null;
        //執(zhí)行腳本
        String cmd = "";

        //獲取平臺(tái)登錄token
        String token = getToken();
        //調(diào)用平臺(tái)文件下載接口獲取文件流
        byte[] download = download(token, type, downloadId);
        if (StringUtils.equals("sql", languageType)) {
            cmd = myCommand;
            fileName = new Date().getTime() + ".sql";
            conn = RemoteCommandConfig.login(myIp, myUsername, myPassword);
            //寫入文件到sql服務(wù)器指定目錄下
            upload(download, myIp, myUsername, myPassword, myFilePath, fileName);
        } else if (StringUtils.equals("python", languageType)) {
            cmd = pyCommand;
            fileName = new Date().getTime() + ".py";
            conn = RemoteCommandConfig.login(pyIp, pyUsername, pyPassword);
            //寫入文件到py服務(wù)器指定目錄下
            upload(download, pyIp, pyUsername, pyPassword, pyFilePath, fileName);
        }
        //執(zhí)行腳本
        return RemoteCommandConfig.execute(conn, cmd + fileName);
    }
}

八、啟動(dòng)類

import com.alibaba.nacos.api.config.ConfigType;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@NacosPropertySource(dataId = "script_****", type = ConfigType.YAML,groupId = "DEFAULT_GROUP",autoRefreshed = true)
public class ScriptDealWithApplication {

    public static void main(String[] args) {
        SpringApplication.run(ScriptDealWithApplication.class, args);
    }

}

九、application.yml配置

server:
  port: 8001
nacos:
  config:
    server-addr: 101.***.**.**:8848  #Nacos 鏈接地址
    namespace: 55572fea-***-********************* #Nacos 命名空間ID

十、nacos配置

gyyh_login:
  ip: http://101.***.***.***:8088/
  username: admin
  password: 123456
mysql_script:
  ip: 192.168.**.***
  username: root
  password: vagrant
  file_path: /usr/local/script/
  command: cd /usr/local/script/ &&mysql -vvv -uroot -p123456 spring_data <
python_script:
  ip: 192.168.**.***
  username: root
  password: vagrant
  file_path: /usr/local/script/
  command: python /usr/local/script/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容