test
This commit is contained in:
commit
2f370b1a30
29
pom.xml
Normal file
29
pom.xml
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.loafle</groupId>
|
||||
<artifactId>maven_parent_jar</artifactId>
|
||||
<version>1.0.0-RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.loafle</groupId>
|
||||
<artifactId>snmp_crawler</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<name>com.loafle.snmp_crawler</name>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/org.snmp4j/snmp4j -->
|
||||
<dependency>
|
||||
<groupId>org.snmp4j</groupId>
|
||||
<artifactId>snmp4j</artifactId>
|
||||
<version>2.5.0</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
23
snmp_crawler.iml
Normal file
23
snmp_crawler.iml
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/target/classes" />
|
||||
<output-test url="file://$MODULE_DIR$/target/test-classes" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/target" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<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" 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" />
|
||||
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-classic:1.1.3" level="project" />
|
||||
<orderEntry type="library" scope="RUNTIME" name="Maven: ch.qos.logback:logback-core:1.1.3" level="project" />
|
||||
</component>
|
||||
</module>
|
71
src/main/java/com/loafle/crawler/snmp/Crawler.java
Normal file
71
src/main/java/com/loafle/crawler/snmp/Crawler.java
Normal file
|
@ -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<String, String> result) {
|
||||
|
||||
for (Map.Entry<String, String> entry : result.entrySet()) {
|
||||
System.out.printf("key: %s - value: %s\n", entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
83
src/main/java/com/loafle/crawler/snmp/SNMP.java
Normal file
83
src/main/java/com/loafle/crawler/snmp/SNMP.java
Normal file
|
@ -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<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.");
|
||||
}
|
||||
|
||||
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<String, String> result = new HashMap<String, String>();
|
||||
for (VariableBinding varBinding : responsePDU.getVariableBindings()) {
|
||||
if (varBinding == null) {
|
||||
continue;
|
||||
}
|
||||
result.put(varBinding.getOid().toString(), varBinding.getVariable().toString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
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.");
|
||||
}
|
||||
|
||||
Map<String, String> result = new HashMap<String, String>();
|
||||
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;
|
||||
}
|
||||
}
|
63
src/main/java/com/loafle/crawler/snmp/SNMPv2c.java
Normal file
63
src/main/java/com/loafle/crawler/snmp/SNMPv2c.java
Normal file
|
@ -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<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());
|
||||
CommunityTarget target = getTarget(addr, port, community);
|
||||
|
||||
return super.walk(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();
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
139
src/main/java/com/loafle/crawler/snmp/SNMPv3.java
Normal file
139
src/main/java/com/loafle/crawler/snmp/SNMPv3.java
Normal file
|
@ -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<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);
|
||||
|
||||
|
||||
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<String, String> get(String addr, String port, String user, int authType, String authPass, int privType, String privPass, String[] oids)
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
0
src/main/resources/_
Normal file
0
src/main/resources/_
Normal file
11
src/test/java/com/loafle/AppTest.java
Normal file
11
src/test/java/com/loafle/AppTest.java
Normal file
|
@ -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");
|
||||
}
|
||||
}
|
17
src/test/resources/logback.xml
Normal file
17
src/test/resources/logback.xml
Normal file
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration scan="true" scanPeriod="3 seconds">
|
||||
<contextName>snmp_crawler</contextName>
|
||||
<!-- TRACE > DEBUG > INFO > WARN > ERROR -->
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{32} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="DEBUG">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
<logger name="com.loafle" level="ALL" />
|
||||
</configuration>
|
Loading…
Reference in New Issue
Block a user