JAVA生成Shell文件并执行

JAVA生成Shell文件并执行

       最近有个奇怪的需求,创建RabbitMq用户,但是不能通过Rabbitmq的管理页面进行添加用户。Rabbitmq的管理页面对外的接口也没有发现添加用户的功能。只能出此下策。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;

public class ShellUtil {
        //创建shell
	public static void createShell(String path, String... strs) throws Exception {

		if (strs == null) {
			System.out.println("strs is null");
			return;
		}

		File sh = new File(path);
		if (sh.exists()) {
			sh.delete();
		}

		sh.createNewFile();
		sh.setExecutable(true);
		FileWriter fw = new FileWriter(sh);
		BufferedWriter bf = new BufferedWriter(fw);

		for (int i = 0; i < strs.length; i++) {
			bf.write(strs[i]);

			if (i < strs.length - 1) {
				bf.newLine();
			}
		}
		bf.flush();
		bf.close();
	}

        //执行shell
	public static String runShell(String shpath) throws Exception {

		if (shpath == null || shpath.equals("")) {
			return "shpath is empty";
		}
		Process ps = Runtime.getRuntime().exec(shpath);
		ps.waitFor();

		BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
		StringBuffer sb = new StringBuffer();
		String line;
		while ((line = br.readLine()) != null) {
			sb.append(line).append("\n");
		}
		String result = sb.toString();
		return result;
	}

        //测试效果
	public static void main(String[] args) {
                //文件存放路径
		String path = "/home/xxx/java.sh";
                //执行的脚本,一个字符串就是一行。
		String[] strs = { "cp aaa.sh aaa2.sh" };
		try {
			ShellUtil.createShell(path, strs);
			String result = ShellUtil.runShell(path);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

自测可用。

你可能感兴趣的:(JAVA)