diff --git a/src/main/java/com/loafle/crawler/snmp/Crawler.java b/src/main/java/com/loafle/crawler/snmp/Crawler.java deleted file mode 100644 index 8ececfb..0000000 --- a/src/main/java/com/loafle/crawler/snmp/Crawler.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.loafle.crawler.snmp; - -import java.util.Map; - -/** - * Created by insanity on 17. 4. 10. - */ -public class 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 static void main(String[] args) { - useV2c(); - useV3(); - } - - private static void useV2c() { - try { - SNMPv2c snmp = new SNMPv2c(); - - Map walkResult = snmp.walk(addr, port, community, "1.3.6.1.2.1.25.2.3.1.3"); - printResult(walkResult); - Map getResult = snmp.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); - - } catch (Exception e) { - System.err.println("An Exception happend as follows."); - System.err.println(e.getMessage()); - e.printStackTrace(); - } - } - - private static void useV3() { - try { - SNMPv3 snmp = new SNMPv3(); - - Map walkResult = snmp.walk(addr, port, user, authType, authPass, privType, privPass, "1.3.6.1.2.1.25.2.3.1.3"); - printResult(walkResult); - Map getResult = snmp.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); - } catch (Exception e) { - System.err.println("An Exception happend as follows."); - System.err.println(e.getMessage()); - e.printStackTrace(); - } - } - - private static void printResult(Map result) { - - for (Map.Entry entry : result.entrySet()) { - System.out.printf("key: %s - value: %s\n", entry.getKey(), entry.getValue()); - } - - } -} \ No newline at end of file diff --git a/src/main/java/com/loafle/overflow/crawler/Crawler.java b/src/main/java/com/loafle/overflow/crawler/Crawler.java new file mode 100644 index 0000000..a198df4 --- /dev/null +++ b/src/main/java/com/loafle/overflow/crawler/Crawler.java @@ -0,0 +1,136 @@ +package com.loafle.overflow.crawler; + +import org.snmp4j.Snmp; +import org.snmp4j.TransportMapping; +import org.snmp4j.smi.Address; +import org.snmp4j.transport.DefaultUdpTransportMapping; + +import java.io.IOException; +import java.util.Map; + +/** + * Created by insanity on 17. 4. 10. + */ +public class 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 static void main(String[] args) { + useV2c(); + useV3(); + } + + private static void useV2c() { + Snmp snmp = null; + TransportMapping transport = null; + try { + transport = new DefaultUdpTransportMapping(); + snmp = new Snmp(transport); + 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); + + } catch (Exception e) { + System.err.println("An Exception happend as follows."); + System.err.println(e.getMessage()); + e.printStackTrace(); + } finally { + if (snmp != null) { + try { + snmp.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (transport != null) { + try { + transport.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + private static void useV3() { + Snmp snmp = null; + TransportMapping transport = null; + + try { + transport = new DefaultUdpTransportMapping(); + snmp = new Snmp(transport); + 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); + + } catch (Exception e) { + System.err.println("An Exception happend as follows."); + System.err.println(e.getMessage()); + e.printStackTrace(); + } finally { + if (snmp != null) { + try { + snmp.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (transport != null) { + try { + transport.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + 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/main/java/com/loafle/crawler/snmp/SNMP.java b/src/main/java/com/loafle/overflow/crawler/SNMP.java similarity index 97% rename from src/main/java/com/loafle/crawler/snmp/SNMP.java rename to src/main/java/com/loafle/overflow/crawler/SNMP.java index 5645b32..af4e70d 100644 --- a/src/main/java/com/loafle/crawler/snmp/SNMP.java +++ b/src/main/java/com/loafle/overflow/crawler/SNMP.java @@ -1,4 +1,4 @@ -package com.loafle.crawler.snmp; +package com.loafle.overflow.crawler; import org.snmp4j.PDU; import org.snmp4j.Snmp; @@ -21,7 +21,6 @@ public abstract class SNMP { protected Map get(Snmp snmp, PDU pdu, Target target) throws Exception { ResponseEvent response = snmp.get(pdu, target); - snmp.close(); if (response == null) { throw new Exception("Error: No response from SNMP Agent."); @@ -53,7 +52,6 @@ public abstract class SNMP { protected Map walk(Snmp snmp, String oidStr, Target target, TreeUtils treeUtils) throws Exception { OID oid = new OID(oidStr); List events = treeUtils.getSubtree(target, oid); - snmp.close(); if(events == null || events.size() == 0){ throw new Exception("No data."); diff --git a/src/main/java/com/loafle/crawler/snmp/SNMPv2c.java b/src/main/java/com/loafle/overflow/crawler/SNMPv2c.java similarity index 67% rename from src/main/java/com/loafle/crawler/snmp/SNMPv2c.java rename to src/main/java/com/loafle/overflow/crawler/SNMPv2c.java index 48be7d9..798f7e5 100644 --- a/src/main/java/com/loafle/crawler/snmp/SNMPv2c.java +++ b/src/main/java/com/loafle/overflow/crawler/SNMPv2c.java @@ -1,4 +1,4 @@ -package com.loafle.crawler.snmp; +package com.loafle.overflow.crawler; /** * Created by insanity on 17. 4. 7. @@ -7,10 +7,8 @@ package com.loafle.crawler.snmp; import org.snmp4j.CommunityTarget; import org.snmp4j.PDU; import org.snmp4j.Snmp; -import org.snmp4j.TransportMapping; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.smi.*; -import org.snmp4j.transport.DefaultUdpTransportMapping; import org.snmp4j.util.DefaultPDUFactory; import org.snmp4j.util.TreeUtils; @@ -18,6 +16,12 @@ import java.util.Map; public class SNMPv2c extends SNMP { + private Snmp snmp = null; + + public SNMPv2c(Snmp snmp) { + this.snmp = snmp; + } + private CommunityTarget getTarget(String addr, String port, String community) { UdpAddress address = new UdpAddress(addr + "/" + port); CommunityTarget target = new CommunityTarget(); @@ -29,25 +33,24 @@ public class SNMPv2c extends SNMP { return target; } + public Boolean validate(String addr, String port, String community) throws Exception { + String testOid = "1.3.6.1.2.1.1.3.0"; + Map result = this.get(addr, port, community, new String[]{testOid}); + if (result.get(testOid) != null) return true; + return false; + } + public Map walk(String addr, String port, String community, String oidStr) throws Exception { - TransportMapping transport = new DefaultUdpTransportMapping(); - Snmp snmp = new Snmp(transport); - transport.listen(); - - TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory()); + TreeUtils treeUtils = new TreeUtils(this.snmp, new DefaultPDUFactory()); CommunityTarget target = getTarget(addr, port, community); - return super.walk(snmp, oidStr, target, treeUtils); + return super.walk(this.snmp, oidStr, target, treeUtils); } public Map get(String addr, String port, String community, String[] oids) throws Exception { - TransportMapping transport = new DefaultUdpTransportMapping(); - Snmp snmp = new Snmp(transport); - transport.listen(); - CommunityTarget target = getTarget(addr, port, community); PDU pdu = new PDU(); @@ -57,7 +60,7 @@ public class SNMPv2c extends SNMP { pdu.setType(PDU.GET); pdu.setRequestID(new Integer32(1)); - return super.get(snmp, pdu, target); + return super.get(this.snmp, pdu, target); } } \ No newline at end of file diff --git a/src/main/java/com/loafle/crawler/snmp/SNMPv3.java b/src/main/java/com/loafle/overflow/crawler/SNMPv3.java similarity index 84% rename from src/main/java/com/loafle/crawler/snmp/SNMPv3.java rename to src/main/java/com/loafle/overflow/crawler/SNMPv3.java index 37b8a01..1a83103 100644 --- a/src/main/java/com/loafle/crawler/snmp/SNMPv3.java +++ b/src/main/java/com/loafle/overflow/crawler/SNMPv3.java @@ -1,4 +1,4 @@ -package com.loafle.crawler.snmp; +package com.loafle.overflow.crawler; import org.snmp4j.*; import org.snmp4j.mp.MPv3; @@ -17,20 +17,29 @@ import java.util.Map; */ public class SNMPv3 extends SNMP { + private Snmp snmp = null; + + public SNMPv3(Snmp snmp) { + this.snmp = snmp; + } + + public Boolean validate(String addr, String port, String user, int authType, String authPass, int privType, String privPass) throws Exception { + String testOid = "1.3.6.1.2.1.1.3.0"; + Map result = this.get(addr, port, user, authType, authPass, privType, privPass, new String[]{testOid}); + if (result.get(testOid) != null) return true; + return false; + } + public Map walk(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String oidStr) throws Exception { - TransportMapping transport = new DefaultUdpTransportMapping(); - Snmp snmp = new Snmp(transport); - transport.listen(); - USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); UsmUser usmUser = getUser(user, authType, authPass, privType, privPass); - snmp.getUSM().addUser(new OctetString(user), usmUser); + this.snmp.getUSM().addUser(new OctetString(user), usmUser); - TreeUtils treeUtils = new TreeUtils(snmp, new PDUFactory() { + TreeUtils treeUtils = new TreeUtils(this.snmp, new PDUFactory() { public PDU createPDU(Target target) { ScopedPDU sp = new ScopedPDU(); sp.setRequestID(new Integer32(1)); @@ -43,7 +52,7 @@ public class SNMPv3 extends SNMP { }); Target target = getTarget(addr, port, usmUser); - return super.walk(snmp, oidStr, target, treeUtils); + return super.walk(this.snmp, oidStr, target, treeUtils); } public Map get(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String[] oids) @@ -57,7 +66,7 @@ public class SNMPv3 extends SNMP { SecurityModels.getInstance().addSecurityModel(usm); UsmUser usmUser = getUser(user, authType, authPass, privType, privPass); - snmp.getUSM().addUser(new OctetString(user), usmUser); + this.snmp.getUSM().addUser(new OctetString(user), usmUser); PDU pdu = new ScopedPDU(); for (String oid : oids) {