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
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;
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<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<>();
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<String> argList = new ArrayList<String>();
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<Map<String,String>> resultMapList = new ArrayList<Map<String, String>>();
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<String,String> 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<String, Integer> metaMap = ofResultSet.getMeta();
List<String> columns = null;
Map<String, String> tempMap = new HashMap<String, String>();
List<String> 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<String, String> data = new HashMap<String, String>();
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;
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<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 null;
return retlist;
}
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 Object processWMI(Config config) throws Exception {
public OFResultSet processWMI(Config config, Item item) throws Exception {
String query = "";
Item cItem = null;
// int idxMetric = 0;
List<OFResultSet> 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();

View File

@ -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();

View File

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