This commit is contained in:
insanity 2017-04-10 19:29:03 +09:00
parent 2f370b1a30
commit f9df081a8f
5 changed files with 172 additions and 97 deletions

View File

@ -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<String, String> result) {
for (Map.Entry<String, String> entry : result.entrySet()) {
System.out.printf("key: %s - value: %s\n", entry.getKey(), entry.getValue());
}
}
}

View File

@ -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<? extends Address> 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<? extends Address> 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<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;
}
}

View File

@ -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<String, String> 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<String, String> walk(Snmp snmp, String oidStr, Target target, TreeUtils treeUtils) throws Exception {
OID oid = new OID(oidStr);
List<TreeEvent> events = treeUtils.getSubtree(target, oid);
snmp.close();
if(events == null || events.size() == 0){
throw new Exception("No data.");

View File

@ -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<String, String> result = this.get(addr, port, community, new String[]{testOid});
if (result.get(testOid) != null) return true;
return false;
}
public Map<String, String> walk(String addr, String port, String community, String oidStr) throws Exception {
TransportMapping<? extends Address> 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<String, String> get(String addr, String port, String community, String[] oids) throws Exception {
TransportMapping<? extends Address> 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);
}
}

View File

@ -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<String, String> result = this.get(addr, port, user, authType, authPass, privType, privPass, new String[]{testOid});
if (result.get(testOid) != null) return true;
return false;
}
public Map<String, String> walk(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String oidStr)
throws Exception {
TransportMapping<? extends Address> 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<String, String> 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) {