diff --git a/pom.xml b/pom.xml index 50b246c..ef90983 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,12 @@ 2.5.0 + + com.loafle + crawler_java + 1.0.0-SNAPSHOT + + \ No newline at end of file diff --git a/snmp_crawler.iml b/snmp_crawler.iml index cc27173..9489d7a 100644 --- a/snmp_crawler.iml +++ b/snmp_crawler.iml @@ -14,6 +14,7 @@ + diff --git a/src/main/java/com/loafle/overflow/crawler/Crawler.java b/src/main/java/com/loafle/overflow/crawler/SNMPCrawler.java similarity index 63% rename from src/main/java/com/loafle/overflow/crawler/Crawler.java rename to src/main/java/com/loafle/overflow/crawler/SNMPCrawler.java index a198df4..8bc949f 100644 --- a/src/main/java/com/loafle/overflow/crawler/Crawler.java +++ b/src/main/java/com/loafle/overflow/crawler/SNMPCrawler.java @@ -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 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 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 result) { - - for (Map.Entry 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; } + } \ No newline at end of file diff --git a/src/test/java/com/loafle/AppTest.java b/src/test/java/com/loafle/AppTest.java index cbadf31..b3c87dd 100644 --- a/src/test/java/com/loafle/AppTest.java +++ b/src/test/java/com/loafle/AppTest.java @@ -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)res); + } + } + + private void printResult(Map result) { + for (Map.Entry entry : result.entrySet()) { + System.out.printf("oid: %s - value: %s\n", entry.getKey(), entry.getValue()); + } + + } + } \ No newline at end of file