This commit is contained in:
insanity 2017-04-11 14:07:57 +09:00
parent f9df081a8f
commit 53d3c7156b
4 changed files with 69 additions and 40 deletions

View File

@ -24,6 +24,12 @@
<version>2.5.0</version> <version>2.5.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.loafle</groupId>
<artifactId>crawler_java</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -14,6 +14,7 @@
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.snmp4j:snmp4j:2.5.0" level="project" /> <orderEntry type="library" name="Maven: org.snmp4j:snmp4j:2.5.0" level="project" />
<orderEntry type="library" name="Maven: log4j:log4j:1.2.14" level="project" /> <orderEntry type="library" name="Maven: log4j:log4j:1.2.14" level="project" />
<orderEntry type="library" name="Maven: com.loafle: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.13" level="project" />

View File

@ -11,7 +11,7 @@ import java.util.Map;
/** /**
* Created by insanity on 17. 4. 10. * Created by insanity on 17. 4. 10.
*/ */
public class Crawler { public class SNMPCrawler extends Crawler {
//v2c //v2c
private static String addr = "192.168.1.215"; private static String addr = "192.168.1.215";
@ -31,12 +31,23 @@ public class Crawler {
private static int privType = 0; private static int privType = 0;
private static String privPass = "qweqwe123"; private static String privPass = "qweqwe123";
public static void main(String[] args) { public enum MethodType {
useV2c(); VALIDATE, GET, WALK;
useV3();
} }
private static void useV2c() { @Override
public Object get(String id) {
//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"});
//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"});
}
private Object getByV2c(MethodType type, String[] oids) {
Snmp snmp = null; Snmp snmp = null;
TransportMapping<? extends Address> transport = null; TransportMapping<? extends Address> transport = null;
try { try {
@ -45,12 +56,16 @@ public class Crawler {
transport.listen(); transport.listen();
SNMPv2c snmpV2 = new SNMPv2c(snmp); SNMPv2c snmpV2 = new SNMPv2c(snmp);
Boolean isOk = snmpV2.validate(addr, port, community); switch (type) {
System.out.println("validate: " + isOk); case VALIDATE:
Map walkResult = snmpV2.walk(addr, port, community, "1.3.6.1.2.1.25.2.3.1.3"); return snmpV2.validate(addr, port, community);
printResult(walkResult); case GET:
Map getResult = snmpV2.get(addr, port, community, new String[]{"1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.2.2.1.6.2"}); return snmpV2.get(addr, port, community, oids);
printResult(getResult); case WALK:
return snmpV2.walk(addr, port, community, oids[0]);
default:
return null;
}
} catch (Exception e) { } catch (Exception e) {
System.err.println("An Exception happend as follows."); System.err.println("An Exception happend as follows.");
@ -72,9 +87,10 @@ public class Crawler {
} }
} }
} }
return null;
} }
private static void useV3() { private Object getByV3(MethodType type, String[] oids) {
Snmp snmp = null; Snmp snmp = null;
TransportMapping<? extends Address> transport = null; TransportMapping<? extends Address> transport = null;
@ -84,12 +100,16 @@ public class Crawler {
transport.listen(); transport.listen();
SNMPv3 snmpV3 = new SNMPv3(snmp); SNMPv3 snmpV3 = new SNMPv3(snmp);
Boolean isOk = snmpV3.validate(addr, port, user, authType, authPass, privType, privPass); switch (type) {
System.out.println("validate: " + isOk); case VALIDATE:
Map walkResult = snmpV3.walk(addr, port, user, authType, authPass, privType, privPass, "1.3.6.1.2.1.25.2.3.1.3"); return snmpV3.validate(addr, port, user, authType, authPass, privType, privPass);
printResult(walkResult); case GET:
Map getResult = snmpV3.get(addr, port, user, authType, authPass, privType, privPass, new String[]{"1.3.6.1.2.1.1.5.0", "1.3.6.1.2.1.2.2.1.6.2"}); return snmpV3.get(addr, port, user, authType, authPass, privType, privPass, oids);
printResult(getResult); case WALK:
return snmpV3.walk(addr, port, user, authType, authPass, privType, privPass, oids[0]);
default:
return null;
}
} catch (Exception e) { } catch (Exception e) {
System.err.println("An Exception happend as follows."); System.err.println("An Exception happend as follows.");
@ -111,26 +131,7 @@ public class Crawler {
} }
} }
} }
}
private static void printResult(Map<String, String> result) {
for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.printf("oid: %s - value: %s\n", entry.getKey(), entry.getValue());
}
}
public String add(String configPath) {
return null;
}
public String init(String configPath) {
return null;
}
public String get(String id) {
return null;
}
public String remove(String id) {
return null; return null;
} }
} }

View File

@ -1,11 +1,32 @@
package com.loafle; package com.loafle;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import com.loafle.overflow.crawler.SNMPCrawler;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
public class AppTest { public class AppTest {
@Test @Test
public void testSum() { public void testSNMP2() {
fail("Not yet implemented"); SNMPCrawler c = new SNMPCrawler();
Object res = c.get("1111");
if(res instanceof Boolean) {
System.out.println("validate : " + res);
assertEquals(res, true);
}else {
printResult((Map<String, String>)res);
}
} }
private void printResult(Map<String, String> result) {
for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.printf("oid: %s - value: %s\n", entry.getKey(), entry.getValue());
}
}
} }