diff --git a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawler.java b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawler.java index eb4d826..add281d 100644 --- a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawler.java +++ b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawler.java @@ -14,6 +14,6 @@ public class WMICrawler extends Crawler { @Override public Object getInternal(Config config) throws Exception { - return WMICrawlerOS.getInstance().processWMI(config); + return WMICrawlerOS.getInstance().process(config); } } diff --git a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerLinux.java b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerLinux.java index 9af667c..b8a8325 100644 --- a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerLinux.java +++ b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerLinux.java @@ -1,6 +1,8 @@ package com.loafle.overflow.crawler.wmi; import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.config.Item; +import com.loafle.overflow.crawler.result.OFResultSet; import java.util.*; @@ -14,28 +16,28 @@ public class WMICrawlerLinux extends WMICrawlerOS { private final String DELIMITER = "||"; private final String DELIMITER_SPLIT = "\\|\\|"; - public Object processWMI(Config config) throws Exception { - return processCommand(config); + public OFResultSet processWMI(Config config, Item item) throws Exception { + + List argList = createCommand(config, item); + + String result = execute(argList.toArray(new String[argList.size()])); + + OFResultSet ofResultSet = parseResult(result, item); + + return ofResultSet; } - public Object processCommand(Config config) throws Exception { + public List createCommand(Config config, Item item) throws Exception { - Map params = new HashMap<>(); - params.put("id", "administrator"); - params.put("pw", "!@#$qwer1234"); - params.put("nameSpace", "root/cimv2"); - params.put("query", "select * from Win32_OperatingSystem"); - params.put("ip", "192.168.1.1"); + String id = (String)config.getTarget().getAuth().get("id"); + String pw = (String)config.getTarget().getAuth().get("pw"); + String nameSpace = (String)item.getQueryInfo().getExtend().get("nameSpace"); + String ip = config.getTarget().getConnection().getIp(); + String query = item.getQueryInfo().getQuery(); - String id = (String)params.get("id"); - String pw = (String)params.get("pw"); - String nameSpace = (String)params.get("nameSpace"); - String query = (String)params.get("query"); - String ip = (String)params.get("ip"); List argList = new ArrayList(); - argList.add("/home/snoop/temp/wmic"); argList.add("-U"); argList.add(id + "%" + pw); @@ -44,11 +46,10 @@ public class WMICrawlerLinux extends WMICrawlerOS { argList.add("--namespace=" + nameSpace); argList.add("--delimiter=" + DELIMITER); - - return execute(argList.toArray(new String[argList.size()])); + return argList; } - public Object execute(String... args) throws Exception { + public String execute(String... args) throws Exception { System.out.println(""); ProcessBuilder builder = new ProcessBuilder(args); Process process = builder.start(); @@ -62,10 +63,10 @@ public class WMICrawlerLinux extends WMICrawlerOS { Arrays.fill(msg, (byte)0); } - return parseResult(buf.toString()); + return buf.toString(); } - public Object parseResult(String result) throws Exception { + public OFResultSet parseResult(String result, Item item) throws Exception { if(result == null || result.length() <= 0) { return null; @@ -73,31 +74,24 @@ public class WMICrawlerLinux extends WMICrawlerOS { result = result.trim(); - List> resultMapList = new ArrayList>(); - - int errIdx = result.indexOf("ERROR:"); - if(errIdx >= 0) { - - int enterIdx = result.indexOf("\\n", errIdx); - if(enterIdx < 0) enterIdx = result.length(); - String errStr = result.substring(errIdx + "ERROR:".length(), enterIdx); - - - Map map = new HashMap<>(); - - map.put("ERROR", errStr); - resultMapList.add(map); - return resultMapList; + String errStr = checkError(result); + if(errStr.length() > 0) { +// map.put("ERROR", errStr); + return null; } String[] lines = result.split("\\n"); String line = ""; - + OFResultSet ofResultSet = OFResultSet.newInstance(item); + Map metaMap = ofResultSet.getMeta(); List columns = null; + Map tempMap = new HashMap(); + List row = null; for (int indexI = 0 ; indexI < lines.length; ++indexI) { + tempMap.clear();; line = lines[indexI].trim(); if (line.length() <= 0) continue; @@ -116,16 +110,43 @@ public class WMICrawlerLinux extends WMICrawlerOS { continue; } + row = new ArrayList<>(); + String[] datas = line.split(DELIMITER_SPLIT); - Map data = new HashMap(); + for(int indexJ = 0; indexJ < columns.size(); ++indexJ) { - data.put(columns.get(indexJ), datas[indexJ]); + tempMap.put(columns.get(indexJ), datas[indexJ]); } - resultMapList.add(data); + for(String key : metaMap.keySet()) { + row.add(tempMap.get(key)); + } + + + ofResultSet.addRow(row); } - return resultMapList; + return ofResultSet; + } + + public String checkError(String result) { + int errIdx = result.indexOf("ERROR:"); + String retErrStr = ""; + if(errIdx >= 0) { + + int enterIdx = result.indexOf("\\n", errIdx); + if(enterIdx < 0) enterIdx = result.length(); + String errStr = result.substring(errIdx + "ERROR:".length(), enterIdx); + + +// Map map = new HashMap<>(); + +// map.put("ERROR", errStr); +// resultMapList.add(map); +// return resultMapList; + retErrStr = errStr; + } + return retErrStr; } } diff --git a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java index fc84c3d..67af391 100644 --- a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java +++ b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerOS.java @@ -1,7 +1,12 @@ package com.loafle.overflow.crawler.wmi; +import com.jacob.activeX.ActiveXComponent; import com.loafle.overflow.crawler.config.Config; +import com.loafle.overflow.crawler.config.Item; +import com.loafle.overflow.crawler.result.OFResultSet; +import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -28,10 +33,21 @@ public abstract class WMICrawlerOS { return WMICrawlerOS.wmiCrawlerOS; } - public Object processWMI(Config config) throws Exception { + public Object process(Config config) throws Exception { + + List retlist = new ArrayList<>(); + ActiveXComponent wmiconnect = null; + + OFResultSet ofResultSet = null; + for(int cIndexI = 0 ; cIndexI < config.getItems().size() ; ++cIndexI) { + + ofResultSet = processWMI(config, config.getItems().get(cIndexI)); + retlist.add(ofResultSet); + } - - return null; + return retlist; } + + public abstract OFResultSet processWMI(Config config, Item item) throws Exception ; } diff --git a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java index ff3e61c..c974ae5 100644 --- a/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java +++ b/src/main/java/com/loafle/overflow/crawler/wmi/WMICrawlerWindows.java @@ -20,59 +20,31 @@ import java.util.Map; */ public class WMICrawlerWindows extends WMICrawlerOS { - public Object processWMI(Config config) throws Exception { + public OFResultSet processWMI(Config config, Item item) throws Exception { String query = ""; - - Item cItem = null; - - -// int idxMetric = 0; - - List retlist = new ArrayList<>(); ActiveXComponent wmiconnect = null; - OFResultSet ofResultSet = null; - for(int cIndexI = 0 ; cIndexI < config.getItems().size() ; ++cIndexI) { - - cItem = config.getItems().get(cIndexI); - - query = cItem.getQueryInfo().getQuery(); - - wmiconnect = connectServer(config, cItem); - - Variant vQuery = new Variant(query); - - Variant vCollection = wmiconnect.invoke("ExecQuery", vQuery); - - wmiconnect.safeRelease(); - vQuery.safeRelease(); - - Dispatch dConnection = vCollection.toDispatch(); - vCollection.safeRelease(); + query = item.getQueryInfo().getQuery(); + wmiconnect = connectServer(config, item); + + Variant vQuery = new Variant(query); + Variant vCollection = wmiconnect.invoke("ExecQuery", vQuery); + wmiconnect.safeRelease(); + vQuery.safeRelease(); + + Dispatch dConnection = vCollection.toDispatch(); + vCollection.safeRelease(); // int count = getCount(dConnection); - EnumVariant enumVariant = new EnumVariant(dConnection); - dConnection.safeRelease(); + EnumVariant enumVariant = new EnumVariant(dConnection); + dConnection.safeRelease(); -// if( count <= 1 ) { -// getSingleValue(enumVariant, cQuery, cItem, idxMetric, resultMap); -// } -// else { -// getMultiValue(enumVariant, cQuery, cItem, idxMetric, resultMap); -// } - - ofResultSet = getResultSet(enumVariant, cItem); - - enumVariant.safeRelease(); -// idxMetric += cQuery.getKeys().size(); - - retlist.add(ofResultSet); - } - - return retlist; + ofResultSet = getResultSet(enumVariant, item); + enumVariant.safeRelease(); + return ofResultSet; } protected OFResultSet getResultSet(EnumVariant enumVariant, Item citem) { @@ -252,7 +224,7 @@ public class WMICrawlerWindows extends WMICrawlerOS { String id = (String)config.getTarget().getAuth().get("id"); String pw = (String)config.getTarget().getAuth().get("pw"); - String nameSpace = item.getQueryInfo().getExtend().get("nameSpace"); + String nameSpace = (String)item.getQueryInfo().getExtend().get("nameSpace"); // String query = (String)config.get.get("query"); String ip = config.getTarget().getConnection().getIp(); diff --git a/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java b/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java index 9f7ec89..fee2ad3 100644 --- a/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java +++ b/src/test/java/com/loafle/overflow/crawler/wmi/WMICrawlerTest.java @@ -129,8 +129,8 @@ public class WMICrawlerTest { // c.getTarget().setAuth(map); - c.getTarget().getAuth().put("id", "administrator"); - c.getTarget().getAuth().put("pw", "!@#$qwer1234"); +// c.getTarget().getAuth().put("id", "administrator"); +// c.getTarget().getAuth().put("pw", "!@#$qwer1234"); WMICrawler wmiCrawler = new WMICrawler(); diff --git a/src/test/resources/config/example.json b/src/test/resources/config/example.json index 74ff15b..b5731c5 100644 --- a/src/test/resources/config/example.json +++ b/src/test/resources/config/example.json @@ -8,7 +8,8 @@ "portType" : "tcp" }, "auth" : { - + "id":"administrator", + "pw":"!@#$qwer1234" } }, "schedule" : {