From 2f370b1a308fcf6e277d1bafd7a48c1df0948d36 Mon Sep 17 00:00:00 2001 From: insanity Date: Mon, 10 Apr 2017 17:32:07 +0900 Subject: [PATCH] test --- pom.xml | 29 ++++ snmp_crawler.iml | 23 +++ .../java/com/loafle/crawler/snmp/Crawler.java | 71 +++++++++ .../java/com/loafle/crawler/snmp/SNMP.java | 83 +++++++++++ .../java/com/loafle/crawler/snmp/SNMPv2c.java | 63 ++++++++ .../java/com/loafle/crawler/snmp/SNMPv3.java | 139 ++++++++++++++++++ src/main/resources/_ | 0 src/test/java/com/loafle/AppTest.java | 11 ++ src/test/resources/logback.xml | 17 +++ 9 files changed, 436 insertions(+) create mode 100644 pom.xml create mode 100644 snmp_crawler.iml create mode 100644 src/main/java/com/loafle/crawler/snmp/Crawler.java create mode 100644 src/main/java/com/loafle/crawler/snmp/SNMP.java create mode 100644 src/main/java/com/loafle/crawler/snmp/SNMPv2c.java create mode 100644 src/main/java/com/loafle/crawler/snmp/SNMPv3.java create mode 100644 src/main/resources/_ create mode 100644 src/test/java/com/loafle/AppTest.java create mode 100644 src/test/resources/logback.xml diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..50b246c --- /dev/null +++ b/pom.xml @@ -0,0 +1,29 @@ + + + 4.0.0 + + + com.loafle + maven_parent_jar + 1.0.0-RELEASE + + + com.loafle + snmp_crawler + jar + 1.0.0-SNAPSHOT + com.loafle.snmp_crawler + + + + + org.snmp4j + snmp4j + 2.5.0 + + + + + \ No newline at end of file diff --git a/snmp_crawler.iml b/snmp_crawler.iml new file mode 100644 index 0000000..cc27173 --- /dev/null +++ b/snmp_crawler.iml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/loafle/crawler/snmp/Crawler.java b/src/main/java/com/loafle/crawler/snmp/Crawler.java new file mode 100644 index 0000000..8ececfb --- /dev/null +++ b/src/main/java/com/loafle/crawler/snmp/Crawler.java @@ -0,0 +1,71 @@ +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/crawler/snmp/SNMP.java b/src/main/java/com/loafle/crawler/snmp/SNMP.java new file mode 100644 index 0000000..5645b32 --- /dev/null +++ b/src/main/java/com/loafle/crawler/snmp/SNMP.java @@ -0,0 +1,83 @@ +package com.loafle.crawler.snmp; + +import org.snmp4j.PDU; +import org.snmp4j.Snmp; +import org.snmp4j.Target; +import org.snmp4j.event.ResponseEvent; +import org.snmp4j.smi.OID; +import org.snmp4j.smi.VariableBinding; +import org.snmp4j.util.TreeEvent; +import org.snmp4j.util.TreeUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by insanity on 17. 4. 10. + */ +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."); + } + + PDU responsePDU = response.getResponse(); + if (responsePDU == null) { + throw new Exception("Error: Response PDU is null"); + } + + int errorStatus = responsePDU.getErrorStatus(); + int errorIndex = responsePDU.getErrorIndex(); + String errorStatusText = responsePDU.getErrorStatusText(); + + if (errorStatus != PDU.noError) { + throw new Exception("Error: " + errorIndex + " " + errorStatusText); + } + + Map result = new HashMap(); + for (VariableBinding varBinding : responsePDU.getVariableBindings()) { + if (varBinding == null) { + continue; + } + result.put(varBinding.getOid().toString(), varBinding.getVariable().toString()); + } + return result; + } + + 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."); + } + + Map result = new HashMap(); + for (TreeEvent event : events) { + if(event == null) { + continue; + } + if (event.isError()) { + System.err.println(event.getErrorMessage()); + continue; + } + + VariableBinding[] varBindings = event.getVariableBindings(); + + for (VariableBinding varBinding : varBindings) { + if (varBinding == null) { + continue; + } + result.put(varBinding.getOid().toString(), varBinding.getVariable().toString()); + } + } + return result; + } +} diff --git a/src/main/java/com/loafle/crawler/snmp/SNMPv2c.java b/src/main/java/com/loafle/crawler/snmp/SNMPv2c.java new file mode 100644 index 0000000..48be7d9 --- /dev/null +++ b/src/main/java/com/loafle/crawler/snmp/SNMPv2c.java @@ -0,0 +1,63 @@ +package com.loafle.crawler.snmp; + +/** + * Created by insanity on 17. 4. 7. + */ + +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; + +import java.util.Map; + +public class SNMPv2c extends SNMP { + + private CommunityTarget getTarget(String addr, String port, String community) { + UdpAddress address = new UdpAddress(addr + "/" + port); + CommunityTarget target = new CommunityTarget(); + target.setCommunity(new OctetString(community)); + target.setAddress(address); + target.setRetries(2); + target.setTimeout(5000); + target.setVersion(SnmpConstants.version2c); + return target; + } + + 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()); + CommunityTarget target = getTarget(addr, port, community); + + return super.walk(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(); + for (String oid : oids) { + pdu.add(new VariableBinding(new OID(oid))); + } + pdu.setType(PDU.GET); + pdu.setRequestID(new Integer32(1)); + + return super.get(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/crawler/snmp/SNMPv3.java new file mode 100644 index 0000000..37b8a01 --- /dev/null +++ b/src/main/java/com/loafle/crawler/snmp/SNMPv3.java @@ -0,0 +1,139 @@ +package com.loafle.crawler.snmp; + +import org.snmp4j.*; +import org.snmp4j.mp.MPv3; +import org.snmp4j.mp.MessageProcessingModel; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.security.*; +import org.snmp4j.smi.*; +import org.snmp4j.transport.DefaultUdpTransportMapping; +import org.snmp4j.util.PDUFactory; +import org.snmp4j.util.TreeUtils; + +import java.util.Map; + +/** + * Created by insanity on 17. 4. 10. + */ +public class SNMPv3 extends SNMP { + + 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); + + + TreeUtils treeUtils = new TreeUtils(snmp, new PDUFactory() { + public PDU createPDU(Target target) { + ScopedPDU sp = new ScopedPDU(); + sp.setRequestID(new Integer32(1)); + return sp; + } + + public PDU createPDU(MessageProcessingModel messageProcessingModel) { + return new ScopedPDU(); + } + }); + + Target target = getTarget(addr, port, usmUser); + return super.walk(snmp, oidStr, target, treeUtils); + } + + public Map get(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String[] oids) + 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); + + PDU pdu = new ScopedPDU(); + for (String oid : oids) { + pdu.addOID(new VariableBinding(new OID(oid))); + } + pdu.setType(PDU.GET); + pdu.setRequestID(new Integer32(1)); + + Target target = getTarget(addr, port, usmUser); + + return super.get(snmp, pdu, target); + } + + private UserTarget getTarget(String addr, String port, UsmUser user) { + + UdpAddress address = new UdpAddress(addr + "/" + port); + UserTarget target = new UserTarget(); + target.setAddress(address); + target.setRetries(2); + target.setTimeout(5000); + target.setVersion(SnmpConstants.version3); + target.setSecurityModel(user.getSecurityModel()); + + int secLv; + OID autyType = user.getAuthenticationProtocol(); + OID privType = user.getPrivacyProtocol(); + if (autyType != null && privType != null) { + secLv = SecurityLevel.AUTH_PRIV; + }else if (autyType != null && privType == null) { + secLv = SecurityLevel.AUTH_NOPRIV; + }else { + secLv = SecurityLevel.NOAUTH_NOPRIV; + } + target.setSecurityLevel(secLv); + target.setSecurityName(new OctetString(user.getSecurityName())); + return target; + } + + private UsmUser getUser(String user, int authType, String authPass, int privType, String privPass) { + OctetString octetUser = new OctetString(user); + OctetString octetAuthPass = authPass.equals("") ? null : new OctetString(authPass); + OctetString octetPrivPass = privPass.equals("") ? null : new OctetString(privPass); + + UsmUser usmUser = new UsmUser(octetUser, getAuthOID(authType), octetAuthPass, getPrivOID(privType), octetPrivPass); + return usmUser; + } + + private OID getAuthOID(int authType) { + OID authOID = null; + switch(authType) { + case 0: + authOID = null; + break; + case 1: + authOID = AuthMD5.ID; + break; + case 2: + authOID = AuthSHA.ID; + break; + } + return authOID; + } + + private OID getPrivOID(int privType) { + OID privOID = null; + switch(privType) { + case 0: + privOID = null; + break; + case 1: + privOID = PrivDES.ID; + break; + case 2: + privOID = PrivAES128.ID; + break; + } + return privOID; + } +} diff --git a/src/main/resources/_ b/src/main/resources/_ new file mode 100644 index 0000000..e69de29 diff --git a/src/test/java/com/loafle/AppTest.java b/src/test/java/com/loafle/AppTest.java new file mode 100644 index 0000000..cbadf31 --- /dev/null +++ b/src/test/java/com/loafle/AppTest.java @@ -0,0 +1,11 @@ + +package com.loafle; +import static org.junit.Assert.*; +import org.junit.Test; + +public class AppTest { + @Test + public void testSum() { + fail("Not yet implemented"); + } +} \ No newline at end of file diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml new file mode 100644 index 0000000..4fc5653 --- /dev/null +++ b/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + snmp_crawler + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n + + + + + + + + + \ No newline at end of file