Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge branch 'dev' of github.com:riskscanner/riskscanner into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
maguohao2017 committed Oct 28, 2021
2 parents ca27626 + 034aae3 commit 9398f01
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,64 @@
* @author maguohao
*/
public class CommandUtils {
/**
* 返回值对象
*
* @param <T>
*/
static class Result<T> {
/**
* 返回数据
*/
private T data;
/**
* 正常200
*/
private Integer code;

/**
* @param data 调用返回值
* @param code 调用是否成功 200 成功其他失败
*/
private Result(T data, Integer code) {
this.data = data;
this.code = code;
}

public static <T> Result<T> ok(T t) {
Result<T> tResult = new Result<>(t, 200);
return tResult;
}

public static <T> Result<T> error(T t) {
Result<T> tResult = new Result<>(t, 200);
return tResult;
}

public Integer getCode() {
return code;
}

public T getData() {
return data;
}
}

/**
* @param command
* @param workDir 工作路径
* @param command shell语句
* @param workDir 工作目录
* @return
* @throws Exception
*/
public static String commonExecCmdWithResult(String command, String workDir) throws Exception {
public static Result<String> commonExecCmdWithResultNew(String command, String workDir) throws Exception {
StringBuilder stringBuilder = new StringBuilder();
Process exec;
if (StringUtils.isNotBlank(workDir)) {
exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command}, null, new File(workDir));
} else {
exec = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", command});
}
Integer code = 200;
try (
BufferedReader reader = new BufferedReader(new InputStreamReader(exec.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(exec.getErrorStream()))
Expand All @@ -34,6 +78,7 @@ public static String commonExecCmdWithResult(String command, String workDir) thr
while ((line = errorReader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
code = 500;
} else {
while ((line = reader.readLine()) != null) {
stringBuilder.append(line + "\n");
Expand All @@ -42,9 +87,28 @@ public static String commonExecCmdWithResult(String command, String workDir) thr
} catch (InterruptedException e) {
throw e;
}
return stringBuilder.toString();
return code.equals(200) ? Result.ok(stringBuilder.toString()) : Result.error(stringBuilder.toString());
}

/**
* @param command
* @param workDir 工作路径
* @throws Exception
*/
public static String commonExecCmdWithResult(String command, String workDir) throws Exception {
Result<String> stringResult = commonExecCmdWithResultNew(command, workDir);
return stringResult.getData();
}

/**
* 写入数据到文件
*
* @param content 写入内容
* @param dirPath 文件目录
* @param fileName 文件名称
* @return 文件目录
* @throws Exception Io异常
*/
public static String saveAsFile(String content, String dirPath, String fileName) throws Exception {
File file = new File(dirPath);
if (!file.exists()) {
Expand Down Expand Up @@ -74,8 +138,8 @@ public static String saveAsFile(String content, String dirPath, String fileName)
/**
* @param command
* @param workDir 工作路径
* @Desc Nuclei 调用命令行工具,Java 调用 Runtime 执行 nuclei 命令会阻塞,所以找了apache-commons-exec异步执行
* @throws Exception
* @Desc Nuclei 调用命令行工具,Java 调用 Runtime 执行 nuclei 命令会阻塞,所以找了apache-commons-exec异步执行
*/
public static String commonExecCmdWithResultByNuclei(String command, String workDir) throws Exception {
FileOutputStream fileOutputStream = null;
Expand All @@ -90,7 +154,7 @@ public static String commonExecCmdWithResultByNuclei(String command, String work
// 创建执行器
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();

ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000);
ExecuteWatchdog watchdog = new ExecuteWatchdog(60 * 1000);
Executor executor = new DefaultExecutor();
executor.setStreamHandler(pumpStreamHandler);
executor.setExitValue(1);
Expand Down
Loading

0 comments on commit 9398f01

Please sign in to comment.