changed linux

config
This commit is contained in:
snoop 2017-04-26 18:19:46 +09:00
parent faef0021ac
commit 9892f8f35a
6 changed files with 102 additions and 92 deletions

View File

@ -14,6 +14,6 @@ public class WMICrawler extends Crawler {
@Override @Override
public Object getInternal(Config config) throws Exception { public Object getInternal(Config config) throws Exception {
return WMICrawlerOS.getInstance().processWMI(config); return WMICrawlerOS.getInstance().process(config);
} }
} }

View File

@ -1,6 +1,8 @@
package com.loafle.overflow.crawler.wmi; package com.loafle.overflow.crawler.wmi;
import com.loafle.overflow.crawler.config.Config; import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.config.Item;
import com.loafle.overflow.crawler.result.OFResultSet;
import java.util.*; import java.util.*;
@ -14,28 +16,28 @@ public class WMICrawlerLinux extends WMICrawlerOS {
private final String DELIMITER = "||"; private final String DELIMITER = "||";
private final String DELIMITER_SPLIT = "\\|\\|"; private final String DELIMITER_SPLIT = "\\|\\|";
public Object processWMI(Config config) throws Exception { public OFResultSet processWMI(Config config, Item item) throws Exception {
return processCommand(config);
List<String> 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<String> createCommand(Config config, Item item) throws Exception {
Map<String, Object> params = new HashMap<>(); String id = (String)config.getTarget().getAuth().get("id");
params.put("id", "administrator"); String pw = (String)config.getTarget().getAuth().get("pw");
params.put("pw", "!@#$qwer1234"); String nameSpace = (String)item.getQueryInfo().getExtend().get("nameSpace");
params.put("nameSpace", "root/cimv2"); String ip = config.getTarget().getConnection().getIp();
params.put("query", "select * from Win32_OperatingSystem"); String query = item.getQueryInfo().getQuery();
params.put("ip", "192.168.1.1");
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<String> argList = new ArrayList<String>(); List<String> argList = new ArrayList<String>();
argList.add("/home/snoop/temp/wmic"); argList.add("/home/snoop/temp/wmic");
argList.add("-U"); argList.add("-U");
argList.add(id + "%" + pw); argList.add(id + "%" + pw);
@ -44,11 +46,10 @@ public class WMICrawlerLinux extends WMICrawlerOS {
argList.add("--namespace=" + nameSpace); argList.add("--namespace=" + nameSpace);
argList.add("--delimiter=" + DELIMITER); argList.add("--delimiter=" + DELIMITER);
return argList;
return execute(argList.toArray(new String[argList.size()]));
} }
public Object execute(String... args) throws Exception { public String execute(String... args) throws Exception {
System.out.println(""); System.out.println("");
ProcessBuilder builder = new ProcessBuilder(args); ProcessBuilder builder = new ProcessBuilder(args);
Process process = builder.start(); Process process = builder.start();
@ -62,10 +63,10 @@ public class WMICrawlerLinux extends WMICrawlerOS {
Arrays.fill(msg, (byte)0); 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) { if(result == null || result.length() <= 0) {
return null; return null;
@ -73,31 +74,24 @@ public class WMICrawlerLinux extends WMICrawlerOS {
result = result.trim(); result = result.trim();
List<Map<String,String>> resultMapList = new ArrayList<Map<String, String>>(); String errStr = checkError(result);
if(errStr.length() > 0) {
int errIdx = result.indexOf("ERROR:"); // map.put("ERROR", errStr);
if(errIdx >= 0) { return null;
int enterIdx = result.indexOf("\\n", errIdx);
if(enterIdx < 0) enterIdx = result.length();
String errStr = result.substring(errIdx + "ERROR:".length(), enterIdx);
Map<String,String> map = new HashMap<>();
map.put("ERROR", errStr);
resultMapList.add(map);
return resultMapList;
} }
String[] lines = result.split("\\n"); String[] lines = result.split("\\n");
String line = ""; String line = "";
OFResultSet ofResultSet = OFResultSet.newInstance(item);
Map<String, Integer> metaMap = ofResultSet.getMeta();
List<String> columns = null; List<String> columns = null;
Map<String, String> tempMap = new HashMap<String, String>();
List<String> row = null;
for (int indexI = 0 ; indexI < lines.length; ++indexI) { for (int indexI = 0 ; indexI < lines.length; ++indexI) {
tempMap.clear();;
line = lines[indexI].trim(); line = lines[indexI].trim();
if (line.length() <= 0) continue; if (line.length() <= 0) continue;
@ -116,16 +110,43 @@ public class WMICrawlerLinux extends WMICrawlerOS {
continue; continue;
} }
row = new ArrayList<>();
String[] datas = line.split(DELIMITER_SPLIT); String[] datas = line.split(DELIMITER_SPLIT);
Map<String, String> data = new HashMap<String, String>();
for(int indexJ = 0; indexJ < columns.size(); ++indexJ) { 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<String,String> map = new HashMap<>();
// map.put("ERROR", errStr);
// resultMapList.add(map);
// return resultMapList;
retErrStr = errStr;
}
return retErrStr;
} }
} }

View File

@ -1,7 +1,12 @@
package com.loafle.overflow.crawler.wmi; package com.loafle.overflow.crawler.wmi;
import com.jacob.activeX.ActiveXComponent;
import com.loafle.overflow.crawler.config.Config; 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; import java.util.Map;
/** /**
@ -28,10 +33,21 @@ public abstract class WMICrawlerOS {
return WMICrawlerOS.wmiCrawlerOS; return WMICrawlerOS.wmiCrawlerOS;
} }
public Object processWMI(Config config) throws Exception { public Object process(Config config) throws Exception {
List<OFResultSet> 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 retlist;
return null;
} }
public abstract OFResultSet processWMI(Config config, Item item) throws Exception ;
} }

View File

@ -20,59 +20,31 @@ import java.util.Map;
*/ */
public class WMICrawlerWindows extends WMICrawlerOS { public class WMICrawlerWindows extends WMICrawlerOS {
public Object processWMI(Config config) throws Exception { public OFResultSet processWMI(Config config, Item item) throws Exception {
String query = ""; String query = "";
Item cItem = null;
// int idxMetric = 0;
List<OFResultSet> retlist = new ArrayList<>();
ActiveXComponent wmiconnect = null; ActiveXComponent wmiconnect = null;
OFResultSet ofResultSet = 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); // int count = getCount(dConnection);
EnumVariant enumVariant = new EnumVariant(dConnection); EnumVariant enumVariant = new EnumVariant(dConnection);
dConnection.safeRelease(); dConnection.safeRelease();
// if( count <= 1 ) { ofResultSet = getResultSet(enumVariant, item);
// getSingleValue(enumVariant, cQuery, cItem, idxMetric, resultMap); enumVariant.safeRelease();
// } return ofResultSet;
// else {
// getMultiValue(enumVariant, cQuery, cItem, idxMetric, resultMap);
// }
ofResultSet = getResultSet(enumVariant, cItem);
enumVariant.safeRelease();
// idxMetric += cQuery.getKeys().size();
retlist.add(ofResultSet);
}
return retlist;
} }
protected OFResultSet getResultSet(EnumVariant enumVariant, Item citem) { 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 id = (String)config.getTarget().getAuth().get("id");
String pw = (String)config.getTarget().getAuth().get("pw"); 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 query = (String)config.get.get("query");
String ip = config.getTarget().getConnection().getIp(); String ip = config.getTarget().getConnection().getIp();

View File

@ -129,8 +129,8 @@ public class WMICrawlerTest {
// c.getTarget().setAuth(map); // c.getTarget().setAuth(map);
c.getTarget().getAuth().put("id", "administrator"); // c.getTarget().getAuth().put("id", "administrator");
c.getTarget().getAuth().put("pw", "!@#$qwer1234"); // c.getTarget().getAuth().put("pw", "!@#$qwer1234");
WMICrawler wmiCrawler = new WMICrawler(); WMICrawler wmiCrawler = new WMICrawler();

View File

@ -8,7 +8,8 @@
"portType" : "tcp" "portType" : "tcp"
}, },
"auth" : { "auth" : {
"id":"administrator",
"pw":"!@#$qwer1234"
} }
}, },
"schedule" : { "schedule" : {