参考 java操作linux,调用shell命令 - 路迢迢 - 博客园
import java.io.InputStreamReader;
import java.io.LineNumberReader;
/**
* @Author: liyue
* @Date: 2021/12/27/10:18
* @Description:
*/
public class ShellUtil {
private static Boolean isPrintShell = Boolean.FALSE;
static {
if (wc("ls |grep printShell") == 1) {
isPrintShell = Boolean.TRUE;
}
}
public static void main(String[] args) {
write("t", "1111111\n2222\n4444");
System.out.println(read("t"));
}
// 创建文件
public static void write(String file, String content) {
exec("cat > " + file + " <<'EOF'\n" + content + "\nEOF");
}
// 读取文件
public static String read(String file) {
return exec("cat " + file);
}
// 统计数量
public static Integer wc(String cmd) {
return Integer.parseInt(exec(cmd + "|wc -l").replaceAll(" ", ""));
}
// 计算大小
// 远程加自定义命令
// KB
public static String remoteSizeExecSh(String ip, int port, String username, String cmd) {
return "ssh -p " + port + " " + username + "@" + ip + " \"du -s " + cmd + "\" |awk '{print $1}'";
}
// 计算大小
// 远程
// KB
public static String remoteSizeExec(String ip, int port, String username, String cmd) {
return exec("ssh -p " + port + " " + username + "@" + ip + " \"du -s " + cmd + "\" |awk '{print $1}'");
}
// 计算大小
// 本地
// KB
public static String localSizeExec(String file) {
return exec("du -s " + file + " |awk '{print $1}'");
}
// 文件上传
public static String upload(String ip, int port, String username, String file, String remotPath) {
return exec("scp -r -P " + port + " " + file + " " + username + "@" + ip + ":" + remotPath);
}
// 文件下载
public static String download(String ip, int port, String username, String localPath, String remotPath) {
return exec("scp -r -P " + port + " " + username + "@" + ip + ":" + remotPath + " " + localPath);
}
// 远程命令
public static String remoteExec(String ip, int port, String username, String cmd) {
return exec("ssh -p " + port + " " + username + "@" + ip + " \"" + cmd + "\"");
}
// 远程解压
public static String remoteExecTar(String ip, int port, String username, String file, String path) {
String cmd = "tar -zxvf " + file + " -C " + path + " >> /dev/null 2>&1";
return exec("ssh -p " + port + " " + username + "@" + ip + " \"" + cmd + "\"");
}
// 远程命令
// 本地追加处理
public static String remoteExecLocal(String ip, int port, String username, String cmd, String localcmd) {
return exec("ssh -p " + port + " " + username + "@" + ip + " \"" + cmd + "\" " + localcmd);
}
// 忽略输出
public static void execNr(String cmd) {
if (isPrintShell) {
System.out.println("\033[37m" + DateUtil.getNowhm() + "|SHELL|开始执行shell,cmd:\n[--------------\n" + cmd + "\n--------------] \033[0m");
}
try {
Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd + " >> /dev/null 2>&1"}, null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
// 执行shell
public static String exec(String cmd) {
if (isPrintShell) {
System.out.println("\033[37m" + DateUtil.getNowhm() + "|SHELL|开始执行shell,cmd:\n[--------------\n" + cmd + "\n--------------] \033[0m");
}
StringBuffer strList = new StringBuffer();
try {
Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd}, null, null);
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
process.waitFor();
while ((line = input.readLine()) != null) {
strList.append(line).append("\n");
}
if (strList.length() > 0) {
strList.delete(strList.length() - 1, strList.length());
}
} catch (Exception e) {
e.printStackTrace();
}
if (isPrintShell) {
System.out.println("\033[37m" + DateUtil.getNowhm() + "|SHELL|shell执行完成,result:\n[--------------\n" + strList + "\n--------------] \033[0m");
}
return strList.toString();
}
}
END。