test
This commit is contained in:
parent
3201bf605c
commit
05832e8df6
4
pom.xml
4
pom.xml
|
@ -11,10 +11,8 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<groupId>com.loafle.overflow</groupId>
|
<groupId>com.loafle.overflow</groupId>
|
||||||
<artifactId>crawler.snmp</artifactId>
|
<artifactId>crawler_snmp</artifactId>
|
||||||
<packaging>jar</packaging>
|
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
<name>com.loafle.overflow.crawler.snmp</name>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- https://mvnrepository.com/artifact/org.snmp4j/snmp4j -->
|
<!-- https://mvnrepository.com/artifact/org.snmp4j/snmp4j -->
|
||||||
|
|
|
@ -17,8 +17,8 @@
|
||||||
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_java:1.0.0-SNAPSHOT" level="project" />
|
<orderEntry type="library" name="Maven: com.loafle.overflow:crawler_java:1.0.0-SNAPSHOT" level="project" />
|
||||||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
|
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
|
||||||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" />
|
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.25" level="project" />
|
||||||
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.1.3" level="project" />
|
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.2.3" level="project" />
|
||||||
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.1.3" level="project" />
|
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
|
@ -16,41 +16,27 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class SNMPCrawler extends Crawler {
|
public class SNMPCrawler extends Crawler {
|
||||||
|
|
||||||
//v2c
|
|
||||||
private static String addr = "192.168.1.215";
|
|
||||||
private static String port = "161";
|
|
||||||
private static String community = "public";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* v3
|
|
||||||
* loafle MD5 "qwer5795" DES "qweqwe123" : AUTHPRIV
|
|
||||||
* loafle2 SHA "qwer5795" AES "qweqwe123" : AUTHPRIV
|
|
||||||
* loafle3 MD5 "qwer5795" : AUTHNOPRIV
|
|
||||||
* loafle4 :NOAUTHNOPRIV
|
|
||||||
*/
|
|
||||||
private static String user = "loafle3";
|
|
||||||
private static int authType = 1;
|
|
||||||
private static String authPass = "qwer5795";
|
|
||||||
private static int privType = 0;
|
|
||||||
private static String privPass = "qweqwe123";
|
|
||||||
|
|
||||||
public enum MethodType {
|
|
||||||
VALIDATE, GET, WALK;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Object getInternal(Map<String, Object> config) {
|
public Object getInternal(Map<String, Object> config) {
|
||||||
//return getByV2c(MethodType.VALIDATE, null);
|
|
||||||
//return getByV2c(MethodType.GET, new String[]{"1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.2.2.1.6.2"});
|
|
||||||
return getByV2c(MethodType.WALK, new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
|
|
||||||
|
|
||||||
|
String targetVer = (String)config.get("version");
|
||||||
|
Object retObj = null;
|
||||||
|
|
||||||
//return getByV3(MethodType.VALIDATE, null);
|
switch (targetVer) {
|
||||||
//return getByV3(MethodType.GET, new String[]{"1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.2.2.1.6.2"});
|
case "v2c":
|
||||||
//return getByV3(MethodType.WALK, new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
|
retObj = getByV2c(config);
|
||||||
|
break;
|
||||||
|
case "v3":
|
||||||
|
retObj = getByV3(config);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object getByV2c(MethodType type, String[] oids) {
|
return retObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Object getByV2c(Map<String, Object> config) {
|
||||||
Snmp snmp = null;
|
Snmp snmp = null;
|
||||||
TransportMapping<? extends Address> transport = null;
|
TransportMapping<? extends Address> transport = null;
|
||||||
try {
|
try {
|
||||||
|
@ -59,13 +45,18 @@ public class SNMPCrawler extends Crawler {
|
||||||
transport.listen();
|
transport.listen();
|
||||||
SNMPv2c snmpV2 = new SNMPv2c(snmp);
|
SNMPv2c snmpV2 = new SNMPv2c(snmp);
|
||||||
|
|
||||||
switch (type) {
|
String ip = (String)config.get("ip");
|
||||||
case VALIDATE:
|
String port = (String)config.get("port");
|
||||||
return snmpV2.validate(addr, port, community);
|
String community = (String)config.get("community");
|
||||||
case GET:
|
String[] oids = (String[])config.get("oids");
|
||||||
return snmpV2.get(addr, port, community, oids);
|
|
||||||
case WALK:
|
switch ((String)config.get("method")) {
|
||||||
return snmpV2.walk(addr, port, community, oids[0]);
|
case "validate":
|
||||||
|
return snmpV2.validate(ip, port, community);
|
||||||
|
case "get":
|
||||||
|
return snmpV2.get(ip, port, community, oids);
|
||||||
|
case "walk":
|
||||||
|
return snmpV2.walk(ip, port, community, oids[0]);
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -93,7 +84,7 @@ public class SNMPCrawler extends Crawler {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object getByV3(MethodType type, String[] oids) {
|
private Object getByV3(Map<String, Object> config) {
|
||||||
Snmp snmp = null;
|
Snmp snmp = null;
|
||||||
TransportMapping<? extends Address> transport = null;
|
TransportMapping<? extends Address> transport = null;
|
||||||
|
|
||||||
|
@ -103,13 +94,22 @@ public class SNMPCrawler extends Crawler {
|
||||||
transport.listen();
|
transport.listen();
|
||||||
SNMPv3 snmpV3 = new SNMPv3(snmp);
|
SNMPv3 snmpV3 = new SNMPv3(snmp);
|
||||||
|
|
||||||
switch (type) {
|
String ip = (String)config.get("ip");
|
||||||
case VALIDATE:
|
String port = (String)config.get("port");
|
||||||
return snmpV3.validate(addr, port, user, authType, authPass, privType, privPass);
|
String user = (String)config.get("user");
|
||||||
case GET:
|
String authType = (String)config.get("authType");
|
||||||
return snmpV3.get(addr, port, user, authType, authPass, privType, privPass, oids);
|
String authPass = (String)config.get("authPass");
|
||||||
case WALK:
|
String privType = (String)config.get("privType");
|
||||||
return snmpV3.walk(addr, port, user, authType, authPass, privType, privPass, oids[0]);
|
String privPass = (String)config.get("privPass");
|
||||||
|
String[] oids = (String[])config.get("oids");
|
||||||
|
|
||||||
|
switch ((String)config.get("method")) {
|
||||||
|
case "validate":
|
||||||
|
return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
|
||||||
|
case "get":
|
||||||
|
return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
|
||||||
|
case "walk":
|
||||||
|
return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import org.snmp4j.smi.VariableBinding;
|
||||||
import org.snmp4j.util.TreeEvent;
|
import org.snmp4j.util.TreeEvent;
|
||||||
import org.snmp4j.util.TreeUtils;
|
import org.snmp4j.util.TreeUtils;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ public abstract class SNMP {
|
||||||
throw new Exception("Error: " + errorIndex + " " + errorStatusText);
|
throw new Exception("Error: " + errorIndex + " " + errorStatusText);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String, String> result = new LinkedHashMap<String, String>();
|
||||||
for (VariableBinding varBinding : responsePDU.getVariableBindings()) {
|
for (VariableBinding varBinding : responsePDU.getVariableBindings()) {
|
||||||
if (varBinding == null) {
|
if (varBinding == null) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -49,7 +49,7 @@ public abstract class SNMP {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<String, String> walk(Snmp snmp, String oidStr, Target target, TreeUtils treeUtils) throws Exception {
|
protected Map<String, String> walk(String oidStr, Target target, TreeUtils treeUtils) throws Exception {
|
||||||
OID oid = new OID(oidStr);
|
OID oid = new OID(oidStr);
|
||||||
List<TreeEvent> events = treeUtils.getSubtree(target, oid);
|
List<TreeEvent> events = treeUtils.getSubtree(target, oid);
|
||||||
|
|
||||||
|
@ -57,7 +57,7 @@ public abstract class SNMP {
|
||||||
throw new Exception("No data.");
|
throw new Exception("No data.");
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> result = new HashMap<String, String>();
|
Map<String, String> result = new LinkedHashMap<String, String>();
|
||||||
for (TreeEvent event : events) {
|
for (TreeEvent event : events) {
|
||||||
if(event == null) {
|
if(event == null) {
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class SNMPv2c extends SNMP {
|
||||||
TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
|
TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
|
||||||
CommunityTarget target = getTarget(addr, port, community);
|
CommunityTarget target = getTarget(addr, port, community);
|
||||||
|
|
||||||
return super.walk(this.snmp, oidStr, target, treeUtils);
|
return super.walk(oidStr, target, treeUtils);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -23,14 +23,14 @@ public class SNMPv3 extends SNMP {
|
||||||
this.snmp = snmp;
|
this.snmp = snmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean validate(String addr, String port, String user, int authType, String authPass, int privType, String privPass) throws Exception {
|
public Boolean validate(String addr, String port, String user, String authType, String authPass, String privType, String privPass) throws Exception {
|
||||||
String testOid = "1.3.6.1.2.1.1.3.0";
|
String testOid = "1.3.6.1.2.1.1.3.0";
|
||||||
Map<String, String> result = this.get(addr, port, user, authType, authPass, privType, privPass, new String[]{testOid});
|
Map<String, String> result = this.get(addr, port, user, authType, authPass, privType, privPass, new String[]{testOid});
|
||||||
if (result.get(testOid) != null) return true;
|
if (result.get(testOid) != null) return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> walk(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String oidStr)
|
public Map<String, String> walk(String addr, String port, String user, String authType, String authPass, String privType, String privPass, String oidStr)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
|
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
|
||||||
SecurityModels.getInstance().addSecurityModel(usm);
|
SecurityModels.getInstance().addSecurityModel(usm);
|
||||||
|
@ -52,10 +52,10 @@ public class SNMPv3 extends SNMP {
|
||||||
});
|
});
|
||||||
|
|
||||||
Target target = getTarget(addr, port, usmUser);
|
Target target = getTarget(addr, port, usmUser);
|
||||||
return super.walk(this.snmp, oidStr, target, treeUtils);
|
return super.walk(oidStr, target, treeUtils);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, String> get(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String[] oids)
|
public Map<String, String> get(String addr, String port, String user, String authType, String authPass, String privType, String privPass, String[] oids)
|
||||||
throws Exception {
|
throws Exception {
|
||||||
|
|
||||||
TransportMapping<? extends Address> transport = new DefaultUdpTransportMapping();
|
TransportMapping<? extends Address> transport = new DefaultUdpTransportMapping();
|
||||||
|
@ -105,7 +105,7 @@ public class SNMPv3 extends SNMP {
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
private UsmUser getUser(String user, int authType, String authPass, int privType, String privPass) {
|
private UsmUser getUser(String user, String authType, String authPass, String privType, String privPass) {
|
||||||
OctetString octetUser = new OctetString(user);
|
OctetString octetUser = new OctetString(user);
|
||||||
OctetString octetAuthPass = authPass.equals("") ? null : new OctetString(authPass);
|
OctetString octetAuthPass = authPass.equals("") ? null : new OctetString(authPass);
|
||||||
OctetString octetPrivPass = privPass.equals("") ? null : new OctetString(privPass);
|
OctetString octetPrivPass = privPass.equals("") ? null : new OctetString(privPass);
|
||||||
|
@ -114,32 +114,32 @@ public class SNMPv3 extends SNMP {
|
||||||
return usmUser;
|
return usmUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OID getAuthOID(int authType) {
|
private OID getAuthOID(String authType) {
|
||||||
OID authOID = null;
|
OID authOID = null;
|
||||||
switch(authType) {
|
switch(authType) {
|
||||||
case 0:
|
case "":
|
||||||
authOID = null;
|
authOID = null;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case "MD5":
|
||||||
authOID = AuthMD5.ID;
|
authOID = AuthMD5.ID;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case "SHA":
|
||||||
authOID = AuthSHA.ID;
|
authOID = AuthSHA.ID;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return authOID;
|
return authOID;
|
||||||
}
|
}
|
||||||
|
|
||||||
private OID getPrivOID(int privType) {
|
private OID getPrivOID(String privType) {
|
||||||
OID privOID = null;
|
OID privOID = null;
|
||||||
switch(privType) {
|
switch(privType) {
|
||||||
case 0:
|
case "":
|
||||||
privOID = null;
|
privOID = null;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case "DES":
|
||||||
privOID = PrivDES.ID;
|
privOID = PrivDES.ID;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case "AES":
|
||||||
privOID = PrivAES128.ID;
|
privOID = PrivAES128.ID;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,13 +5,48 @@ import static org.junit.Assert.*;
|
||||||
import com.loafle.overflow.crawler.snmp.SNMPCrawler;
|
import com.loafle.overflow.crawler.snmp.SNMPCrawler;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.SortedSet;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
|
/***
|
||||||
|
* v2c test info
|
||||||
|
* private static String addr = "192.168.1.215";
|
||||||
|
* private static String port = "161";
|
||||||
|
* private static String community = "public";
|
||||||
|
*
|
||||||
|
* v3 test info
|
||||||
|
* loafle MD5 "qwer5795" DES "qweqwe123" : AUTHPRIV
|
||||||
|
* loafle2 SHA "qwer5795" AES "qweqwe123" : AUTHPRIV
|
||||||
|
* loafle3 MD5 "qwer5795" : AUTHNOPRIV
|
||||||
|
* loafle4 :NOAUTHNOPRIV
|
||||||
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void testSNMP2() {
|
public void testSNMP() {
|
||||||
SNMPCrawler c = new SNMPCrawler();
|
SNMPCrawler c = new SNMPCrawler();
|
||||||
Object res = c.get("1111");
|
Map config = new HashMap();
|
||||||
|
|
||||||
|
config.put("ip", "192.168.1.215");
|
||||||
|
config.put("port", "161");
|
||||||
|
|
||||||
|
// config.put("version", "v2c");
|
||||||
|
// config.put("community", "public");
|
||||||
|
// config.put("method", "get");
|
||||||
|
// config.put("oids", new String[]{"1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.2.2.1.6.2"});
|
||||||
|
|
||||||
|
config.put("version", "v3");
|
||||||
|
config.put("user", "loafle");
|
||||||
|
config.put("authType", "MD5");
|
||||||
|
config.put("authPass", "qwer5795");
|
||||||
|
config.put("privType", "DES");
|
||||||
|
config.put("privPass", "qweqwe123");
|
||||||
|
config.put("method", "walk");
|
||||||
|
config.put("oids", new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
|
||||||
|
|
||||||
|
Object res = c.getInternal(config);
|
||||||
|
|
||||||
if(res instanceof Boolean) {
|
if(res instanceof Boolean) {
|
||||||
System.out.println("validate : " + res);
|
System.out.println("validate : " + res);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user