This commit is contained in:
jackdaw@loafle.com 2017-04-14 10:43:27 +09:00
parent 803554f335
commit 3388ea258a
2 changed files with 110 additions and 109 deletions

View File

@ -1,6 +1,7 @@
package com.loafle.overflow.crawler.snmp;
import com.loafle.overflow.crawler.Crawler;
import com.loafle.overflow.crawler.config.Config;
import com.loafle.overflow.crawler.snmp.version.SNMPv2c;
import com.loafle.overflow.crawler.snmp.version.SNMPv3;
import org.snmp4j.Snmp;
@ -17,121 +18,121 @@ import java.util.Map;
public class SNMPCrawler extends Crawler {
@Override
public Object getInternal(Map<String, Object> config) {
public Object getInternal(Config config) {
String targetVer = (String)config.get("version");
Object retObj = null;
switch (targetVer) {
case "v2c":
retObj = getByV2c(config);
break;
case "v3":
retObj = getByV3(config);
break;
default:
new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
}
// String targetVer = (String)config.get("version");
// switch (targetVer) {
// case "v2c":
// retObj = getByV2c(config);
// break;
// case "v3":
// retObj = getByV3(config);
// break;
// default:
// new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
// }
return retObj;
}
private Object getByV2c(Map<String, Object> config) {
private Object getByV2c(Config config) {
Snmp snmp = null;
TransportMapping<? extends Address> transport = null;
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
SNMPv2c snmpV2 = new SNMPv2c(snmp);
String ip = (String)config.get("ip");
String port = (String)config.get("port");
String community = (String)config.get("community");
String[] oids = (String[])config.get("oids");
String method = (String)config.get("method");
switch (method) {
case "validate":
return snmpV2.validate(ip, port, community);
case "get":
return snmpV2.get(ip, port, community, oids);
case "walk":
return snmpV2.walk(ip, port, community, oids[0]);
default:
new Exception("Unknown SNMP command : " + method).printStackTrace();
}
} catch (Exception e) {
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();
}
}
}
// TransportMapping<? extends Address> transport = null;
// try {
// transport = new DefaultUdpTransportMapping();
// snmp = new Snmp(transport);
// transport.listen();
// SNMPv2c snmpV2 = new SNMPv2c(snmp);
//
// String ip = (String)config.get("ip");
// String port = (String)config.get("port");
// String community = (String)config.get("community");
// String[] oids = (String[])config.get("oids");
// String method = (String)config.get("method");
//
// switch (method) {
// case "validate":
// return snmpV2.validate(ip, port, community);
// case "get":
// return snmpV2.get(ip, port, community, oids);
// case "walk":
// return snmpV2.walk(ip, port, community, oids[0]);
// default:
// new Exception("Unknown SNMP command : " + method).printStackTrace();
// }
//
// } catch (Exception e) {
// 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();
// }
// }
// }
return null;
}
private Object getByV3(Map<String, Object> config) {
private Object getByV3(Config config) {
Snmp snmp = null;
TransportMapping<? extends Address> transport = null;
try {
transport = new DefaultUdpTransportMapping();
snmp = new Snmp(transport);
transport.listen();
SNMPv3 snmpV3 = new SNMPv3(snmp);
String ip = (String)config.get("ip");
String port = (String)config.get("port");
String user = (String)config.get("user");
String authType = (String)config.get("authType");
String authPass = (String)config.get("authPass");
String privType = (String)config.get("privType");
String privPass = (String)config.get("privPass");
String[] oids = (String[])config.get("oids");
String method = (String)config.get("method");
switch (method) {
case "validate":
return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
case "get":
return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
case "walk":
return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
default:
new Exception("Unknown SNMP command : " + method).printStackTrace();
}
} catch (Exception e) {
new Exception("An Exception happend : " + e.getMessage()).printStackTrace();
} finally {
if (snmp != null) {
try {
snmp.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (transport != null) {
try {
transport.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// try {
// transport = new DefaultUdpTransportMapping();
// snmp = new Snmp(transport);
// transport.listen();
// SNMPv3 snmpV3 = new SNMPv3(snmp);
//
// String ip = (String)config.get("ip");
// String port = (String)config.get("port");
// String user = (String)config.get("user");
// String authType = (String)config.get("authType");
// String authPass = (String)config.get("authPass");
// String privType = (String)config.get("privType");
// String privPass = (String)config.get("privPass");
// String[] oids = (String[])config.get("oids");
// String method = (String)config.get("method");
//
// switch (method) {
// case "validate":
// return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
// case "get":
// return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
// case "walk":
// return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
// default:
// new Exception("Unknown SNMP command : " + method).printStackTrace();
// }
//
// } catch (Exception e) {
// new Exception("An Exception happend : " + e.getMessage()).printStackTrace();
// } finally {
// if (snmp != null) {
// try {
// snmp.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// if (transport != null) {
// try {
// transport.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
return null;
}
}

View File

@ -46,13 +46,13 @@ public class AppTest {
config.put("method", "walk");
config.put("oids", new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
Object result = c.getInternal(config);
if(result instanceof Boolean) {
System.out.println("validate : " + result);
}else {
printResult((Map<String, String>)result);
}
// Object result = c.getInternal(config);
//
// if(result instanceof Boolean) {
// System.out.println("validate : " + result);
// }else {
// printResult((Map<String, String>)result);
// }
}