import org.apache.commons.lang3.SystemUtils;
import java.io.*;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
-
@author zhenghongjiu
-
@version 1.0
-
@date 2019/12/4 16:22
*/
public class LinuxWindows {
public static void callCmd(String url, String path) throws Exception {
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
StringBuilder sb = null;
BufferedWriter out = null;
Process process=null;
Date date = new Date();SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-ddHHmmss");//文件名称格式 String dateString = formatter.format(date); File file = new File("" + path + "" + dateString + ".txt");// 創建文件 FileOutputStream files = new FileOutputStream(file); if (SystemUtils.IS_OS_LINUX) {//判断操作系统 List<String> command = new ArrayList<String>(); String str1 = "date +%Y-%m-%d%t%H:%M:%S.%N%t%z%t%Z";//时间格式 String str2 = "uname -a";//取证服务器操作系统信息 String str3 = "ifconfig";//取证服务器网络接口信息 String str4 = "traceroute -m 10 " + url;//被取证网站路由跟踪 String str5 = "nslookup " + url;//被取证网站域名查询信息 String str6 = "date +%Y-%m-%d%t%H:%M:%S.%N%t%z%t%Z"; command.add(str1);//命令放到集合中 command.add(str2); command.add(str3); command.add(str4); command.add(str5); command.add(str6); try { for (int i = 0; i < command.size(); i++) { process = Runtime.getRuntime().exec(command.get(i)); int exitCode = process.waitFor(); // 取得命令结果的输出流 is = process.getInputStream(); // 用一个读输出流类去读 isr = new InputStreamReader(is); //写入文件 out = new BufferedWriter(new OutputStreamWriter(files, "UTF-8")); // 用缓冲器读行 br = new BufferedReader(isr); String line = null; sb = new StringBuilder(); // 生成文件注釋信息 if (i == 0) { out.write("# ====开始取证时间:===="); out.append("\r\n"); } else if (i == 1) { out.write("# ====取证服务器操作系统信息:===="); out.append("\r\n"); } else if (i == 2) { out.write("# ====取证服务器网络接口信息:===="); out.append("\r\n"); } else if (i == 3) { out.write("# ====被取证网站路由跟踪:===="); out.append("\r\n"); } else if (i == 4) { out.write("# ====被取证网站域名查询信息:===="); out.append("\r\n"); } else { out.write("# ====结束取证时间:===="); out.append("\r\n"); } while ((line = br.readLine()) != null) { out.write(line); out.newLine(); out.flush(); sb.append(line); } } } catch (java.lang.NullPointerException e) { System.err.println("NullPointerException " + e.getMessage()); } finally { is.close(); isr.close(); br.close(); out.close(); process.destroy(); } } // 使用Runtime来执行command,生成Process对象 if (SystemUtils.IS_OS_WINDOWS) { String[] param_array=null; Instant timestamp = Instant.now();//时间戳 List<String> cmd = new ArrayList<String>(); String str1 = "systeminfo";//服务器操作系统信息 String str2 = "ipconfig";//服务器网络接口信息 String str3 = "tracert -h 10 " + url;//被取证网站路由跟踪 String str4 = "nslookup " + url;//被取证网站域名查询信息 cmd.add(str1);//命令放到集合中 cmd.add(str2); cmd.add(str3); cmd.add(str4); try { for (int i = 0; i < cmd.size(); i++) { param_array = cmd.get(i).split("[\\s]+"); ProcessBuilder pb = new ProcessBuilder(param_array); process = pb.start(); is = process.getInputStream(); //设置编码 isr = new InputStreamReader(is, "GBK"); //写入文件 out = new BufferedWriter(new OutputStreamWriter(files, "UTF-8")); br = new BufferedReader(isr); String line; sb = new StringBuilder(); // 生成文件注釋信息 if (i == 0) { out.write("# ====取证服务器操作系统信息:===="); out.append("\r\n"); } else if (i == 1) { out.write("# ====取证服务器网络接口信息:===="); out.append("\r\n"); } else if (i == 2) { out.write("# ====被取证网站路由跟踪:===="); out.append("\r\n"); } else { out.write("# ====被取证网站域名查询信息:===="); out.append("\r\n"); } while ((line = br.readLine()) != null) { //写入文件 out.write(line); out.newLine(); out.flush(); sb.append(line); } } } catch (Exception e) { System.err.println("Exception " + e.getMessage()); } finally { Instant timestamp2 = Instant.now(); /* 根据时间获取取证的时间写入文件 */ out.write("# ====取证开始时间:===="); out.append("\r\n"); out.write(String.valueOf(timestamp)); out.append("\r\n"); out.write("# ====取证结束时间:===="); out.append("\r\n"); out.write(String.valueOf(timestamp2)); //关闭 避免浪费资源 is.close(); isr.close(); br.close(); out.close(); } }
}
}