test
This commit is contained in:
parent
f9df081a8f
commit
53d3c7156b
6
pom.xml
6
pom.xml
|
@ -24,6 +24,12 @@
|
|||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.loafle</groupId>
|
||||
<artifactId>crawler_java</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -14,6 +14,7 @@
|
|||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<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: 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: org.hamcrest:hamcrest-core:1.3" level="project" />
|
||||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" />
|
||||
|
|
|
@ -11,7 +11,7 @@ import java.util.Map;
|
|||
/**
|
||||
* Created by insanity on 17. 4. 10.
|
||||
*/
|
||||
public class Crawler {
|
||||
public class SNMPCrawler extends Crawler {
|
||||
|
||||
//v2c
|
||||
private static String addr = "192.168.1.215";
|
||||
|
@ -31,12 +31,23 @@ public class Crawler {
|
|||
private static int privType = 0;
|
||||
private static String privPass = "qweqwe123";
|
||||
|
||||
public static void main(String[] args) {
|
||||
useV2c();
|
||||
useV3();
|
||||
public enum MethodType {
|
||||
VALIDATE, GET, WALK;
|
||||
}
|
||||
|
||||
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;
|
||||
TransportMapping<? extends Address> transport = null;
|
||||
try {
|
||||
|
@ -45,12 +56,16 @@ public class Crawler {
|
|||
transport.listen();
|
||||
SNMPv2c snmpV2 = new SNMPv2c(snmp);
|
||||
|
||||
Boolean isOk = snmpV2.validate(addr, port, community);
|
||||
System.out.println("validate: " + isOk);
|
||||
Map walkResult = snmpV2.walk(addr, port, community, "1.3.6.1.2.1.25.2.3.1.3");
|
||||
printResult(walkResult);
|
||||
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"});
|
||||
printResult(getResult);
|
||||
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]);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
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;
|
||||
TransportMapping<? extends Address> transport = null;
|
||||
|
||||
|
@ -84,12 +100,16 @@ public class Crawler {
|
|||
transport.listen();
|
||||
SNMPv3 snmpV3 = new SNMPv3(snmp);
|
||||
|
||||
Boolean isOk = snmpV3.validate(addr, port, user, authType, authPass, privType, privPass);
|
||||
System.out.println("validate: " + isOk);
|
||||
Map walkResult = snmpV3.walk(addr, port, user, authType, authPass, privType, privPass, "1.3.6.1.2.1.25.2.3.1.3");
|
||||
printResult(walkResult);
|
||||
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"});
|
||||
printResult(getResult);
|
||||
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]);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
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());
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,32 @@
|
|||
|
||||
package com.loafle;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import com.loafle.overflow.crawler.SNMPCrawler;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void testSum() {
|
||||
fail("Not yet implemented");
|
||||
public void testSNMP2() {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user