欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java调用cmd以及liunx命令工具类

程序员文章站 2024-02-21 09:49:22
...
package com.citydo.xclouddesk.utils;

import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.nio.charset.Charset;
import java.util.List;

/**
 * @author nick
 */
@Slf4j
public class LinuxUtil {

    public static void main(String[] args) {
        String cmd = "dir";
        LinuxUtil.executeCmd(cmd);
        LinuxUtil.exec(cmd);
        List<String> commands = new ArrayList<>();
        commands.add("cd /home");
        commands.add("ll");
        commands.add("ls");
        LinuxUtil.execFlow(commands);

    }

    /**
     * 批量 liunx命令
     * @param commands
     * @return
     */
    public static Object execFlow(List<String> commands) {
        try {
            String cmds = "";
            for (String cmd : commands) {
                cmds += cmd + ";";
            }
            String[] cmdA = {"/bin/sh", "-c", cmds};
            Process process = Runtime.getRuntime().exec(cmdA);
            LineNumberReader br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream(), Charset.forName("GBK")));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                log.info("输出结果:{}",line);
                sb.append(line).append("\n");
            }
            br.close();
            process.waitFor();
            return sb.toString();
        } catch (Exception e) {
            log.info("Linux命令异常:{}",e.getMessage());
        }
        return null;
    }

    /**
     * 单条liunx命令
     * @param cmd
     * @return
     */
    public static Object exec(String cmd) {
        try {
            String[] cmdA = {"/bin/sh", "-c", cmd};
            Process process = Runtime.getRuntime().exec(cmdA);
            LineNumberReader br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream(), Charset.forName("GBK")));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = br.readLine()) != null) {
                log.info("输出结果:{}",line);
                sb.append(line).append("\n");
            }
            br.close();
            process.waitFor();
            return sb.toString();
        } catch (Exception e) {
            log.info("Linux命令异常:{}",e.getMessage());
        }
        return null;
    }


    /**
     * InputStream inStream = p.getInputStream();
     * InputStreamReader inReader = new InputStreamReader(inStream);
     * BufferedReader inBuffer = new BufferedReader(inReader);
     * @param cmdStr  CMD命令字符
     */
    public static Object executeCmd(String cmdStr) {
        Runtime run = Runtime.getRuntime();
        try {
            Process process = run.exec("cmd.exe /c " + cmdStr);
            LineNumberReader br = new LineNumberReader(new InputStreamReader(
                    process.getInputStream(), Charset.forName("GBK")));
            StringBuffer sb = new StringBuffer();
            String line;
            while((line = br.readLine()) != null){
                log.info("输出结果:{}",line);
                sb.append(line).append("\n");
            }
            br.close();
            process.waitFor();
            return sb.toString();
        } catch (Exception e) {
            log.info("cmd命令异常:{}",e.getMessage());
        }
        return null;
    }


}

相关标签: 服务器运维