This commit is contained in:
insanity 2017-04-12 14:04:24 +09:00
parent 3201bf605c
commit 05832e8df6
7 changed files with 104 additions and 71 deletions

View File

@ -11,10 +11,8 @@
</parent>
<groupId>com.loafle.overflow</groupId>
<artifactId>crawler.snmp</artifactId>
<packaging>jar</packaging>
<artifactId>crawler_snmp</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>com.loafle.overflow.crawler.snmp</name>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.snmp4j/snmp4j -->

View File

@ -17,8 +17,8 @@
<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: 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" 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-core:1.1.3" 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.2.3" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.2.3" level="project" />
</component>
</module>

View File

@ -16,41 +16,27 @@ import java.util.Map;
*/
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
protected 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"});
public Object getInternal(Map<String, Object> config) {
String targetVer = (String)config.get("version");
Object retObj = null;
//return getByV3(MethodType.VALIDATE, null);
//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"});
//return getByV3(MethodType.WALK, new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
switch (targetVer) {
case "v2c":
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;
TransportMapping<? extends Address> transport = null;
try {
@ -59,13 +45,18 @@ public class SNMPCrawler extends Crawler {
transport.listen();
SNMPv2c snmpV2 = new SNMPv2c(snmp);
switch (type) {
case VALIDATE:
return snmpV2.validate(addr, port, community);
case GET:
return snmpV2.get(addr, port, community, oids);
case WALK:
return snmpV2.walk(addr, port, community, oids[0]);
String ip = (String)config.get("ip");
String port = (String)config.get("port");
String community = (String)config.get("community");
String[] oids = (String[])config.get("oids");
switch ((String)config.get("method")) {
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:
return null;
}
@ -93,7 +84,7 @@ public class SNMPCrawler extends Crawler {
return null;
}
private Object getByV3(MethodType type, String[] oids) {
private Object getByV3(Map<String, Object> config) {
Snmp snmp = null;
TransportMapping<? extends Address> transport = null;
@ -103,13 +94,22 @@ public class SNMPCrawler extends Crawler {
transport.listen();
SNMPv3 snmpV3 = new SNMPv3(snmp);
switch (type) {
case VALIDATE:
return snmpV3.validate(addr, port, user, authType, authPass, privType, privPass);
case GET:
return snmpV3.get(addr, port, user, authType, authPass, privType, privPass, oids);
case WALK:
return snmpV3.walk(addr, port, user, authType, authPass, privType, privPass, oids[0]);
String ip = (String)config.get("ip");
String port = (String)config.get("port");
String user = (String)config.get("user");
String authType = (String)config.get("authType");
String authPass = (String)config.get("authPass");
String privType = (String)config.get("privType");
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:
return null;
}

View File

@ -9,7 +9,7 @@ import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.TreeEvent;
import org.snmp4j.util.TreeUtils;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -39,7 +39,7 @@ public abstract class SNMP {
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()) {
if (varBinding == null) {
continue;
@ -49,7 +49,7 @@ public abstract class SNMP {
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);
List<TreeEvent> events = treeUtils.getSubtree(target, oid);
@ -57,7 +57,7 @@ public abstract class SNMP {
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) {
if(event == null) {
continue;

View File

@ -45,7 +45,7 @@ public class SNMPv2c extends SNMP {
TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory());
CommunityTarget target = getTarget(addr, port, community);
return super.walk(this.snmp, oidStr, target, treeUtils);
return super.walk(oidStr, target, treeUtils);
}

View File

@ -23,14 +23,14 @@ public class SNMPv3 extends 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";
Map<String, String> result = this.get(addr, port, user, authType, authPass, privType, privPass, new String[]{testOid});
if (result.get(testOid) != null) return true;
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 {
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
@ -52,10 +52,10 @@ public class SNMPv3 extends SNMP {
});
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 {
TransportMapping<? extends Address> transport = new DefaultUdpTransportMapping();
@ -105,7 +105,7 @@ public class SNMPv3 extends SNMP {
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 octetAuthPass = authPass.equals("") ? null : new OctetString(authPass);
OctetString octetPrivPass = privPass.equals("") ? null : new OctetString(privPass);
@ -114,32 +114,32 @@ public class SNMPv3 extends SNMP {
return usmUser;
}
private OID getAuthOID(int authType) {
private OID getAuthOID(String authType) {
OID authOID = null;
switch(authType) {
case 0:
case "":
authOID = null;
break;
case 1:
case "MD5":
authOID = AuthMD5.ID;
break;
case 2:
case "SHA":
authOID = AuthSHA.ID;
break;
}
return authOID;
}
private OID getPrivOID(int privType) {
private OID getPrivOID(String privType) {
OID privOID = null;
switch(privType) {
case 0:
case "":
privOID = null;
break;
case 1:
case "DES":
privOID = PrivDES.ID;
break;
case 2:
case "AES":
privOID = PrivAES128.ID;
break;
}

View File

@ -5,13 +5,48 @@ import static org.junit.Assert.*;
import com.loafle.overflow.crawler.snmp.SNMPCrawler;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
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
public void testSNMP2() {
public void testSNMP() {
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) {
System.out.println("validate : " + res);