.
This commit is contained in:
parent
803554f335
commit
3388ea258a
|
@ -1,6 +1,7 @@
|
||||||
package com.loafle.overflow.crawler.snmp;
|
package com.loafle.overflow.crawler.snmp;
|
||||||
|
|
||||||
import com.loafle.overflow.crawler.Crawler;
|
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.SNMPv2c;
|
||||||
import com.loafle.overflow.crawler.snmp.version.SNMPv3;
|
import com.loafle.overflow.crawler.snmp.version.SNMPv3;
|
||||||
import org.snmp4j.Snmp;
|
import org.snmp4j.Snmp;
|
||||||
|
@ -17,121 +18,121 @@ import java.util.Map;
|
||||||
public class SNMPCrawler extends Crawler {
|
public class SNMPCrawler extends Crawler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getInternal(Map<String, Object> config) {
|
public Object getInternal(Config config) {
|
||||||
|
|
||||||
|
|
||||||
String targetVer = (String)config.get("version");
|
|
||||||
Object retObj = null;
|
Object retObj = null;
|
||||||
|
// String targetVer = (String)config.get("version");
|
||||||
switch (targetVer) {
|
// switch (targetVer) {
|
||||||
case "v2c":
|
// case "v2c":
|
||||||
retObj = getByV2c(config);
|
// retObj = getByV2c(config);
|
||||||
break;
|
// break;
|
||||||
case "v3":
|
// case "v3":
|
||||||
retObj = getByV3(config);
|
// retObj = getByV3(config);
|
||||||
break;
|
// break;
|
||||||
default:
|
// default:
|
||||||
new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
|
// new Exception("Unknown SNMP protocol : " + targetVer).printStackTrace();
|
||||||
}
|
// }
|
||||||
|
|
||||||
return retObj;
|
return retObj;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object getByV2c(Map<String, Object> config) {
|
private Object getByV2c(Config config) {
|
||||||
Snmp snmp = null;
|
Snmp snmp = null;
|
||||||
TransportMapping<? extends Address> transport = null;
|
// TransportMapping<? extends Address> transport = null;
|
||||||
try {
|
// try {
|
||||||
transport = new DefaultUdpTransportMapping();
|
// transport = new DefaultUdpTransportMapping();
|
||||||
snmp = new Snmp(transport);
|
// snmp = new Snmp(transport);
|
||||||
transport.listen();
|
// transport.listen();
|
||||||
SNMPv2c snmpV2 = new SNMPv2c(snmp);
|
// SNMPv2c snmpV2 = new SNMPv2c(snmp);
|
||||||
|
//
|
||||||
String ip = (String)config.get("ip");
|
// String ip = (String)config.get("ip");
|
||||||
String port = (String)config.get("port");
|
// String port = (String)config.get("port");
|
||||||
String community = (String)config.get("community");
|
// String community = (String)config.get("community");
|
||||||
String[] oids = (String[])config.get("oids");
|
// String[] oids = (String[])config.get("oids");
|
||||||
String method = (String)config.get("method");
|
// String method = (String)config.get("method");
|
||||||
|
//
|
||||||
switch (method) {
|
// switch (method) {
|
||||||
case "validate":
|
// case "validate":
|
||||||
return snmpV2.validate(ip, port, community);
|
// return snmpV2.validate(ip, port, community);
|
||||||
case "get":
|
// case "get":
|
||||||
return snmpV2.get(ip, port, community, oids);
|
// return snmpV2.get(ip, port, community, oids);
|
||||||
case "walk":
|
// case "walk":
|
||||||
return snmpV2.walk(ip, port, community, oids[0]);
|
// return snmpV2.walk(ip, port, community, oids[0]);
|
||||||
default:
|
// default:
|
||||||
new Exception("Unknown SNMP command : " + method).printStackTrace();
|
// new Exception("Unknown SNMP command : " + method).printStackTrace();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
} finally {
|
// } finally {
|
||||||
if (snmp != null) {
|
// if (snmp != null) {
|
||||||
try {
|
// try {
|
||||||
snmp.close();
|
// snmp.close();
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
if (transport != null) {
|
// if (transport != null) {
|
||||||
try {
|
// try {
|
||||||
transport.close();
|
// transport.close();
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object getByV3(Map<String, Object> config) {
|
private Object getByV3(Config config) {
|
||||||
Snmp snmp = null;
|
Snmp snmp = null;
|
||||||
TransportMapping<? extends Address> transport = null;
|
TransportMapping<? extends Address> transport = null;
|
||||||
|
|
||||||
try {
|
// try {
|
||||||
transport = new DefaultUdpTransportMapping();
|
// transport = new DefaultUdpTransportMapping();
|
||||||
snmp = new Snmp(transport);
|
// snmp = new Snmp(transport);
|
||||||
transport.listen();
|
// transport.listen();
|
||||||
SNMPv3 snmpV3 = new SNMPv3(snmp);
|
// SNMPv3 snmpV3 = new SNMPv3(snmp);
|
||||||
|
//
|
||||||
String ip = (String)config.get("ip");
|
// String ip = (String)config.get("ip");
|
||||||
String port = (String)config.get("port");
|
// String port = (String)config.get("port");
|
||||||
String user = (String)config.get("user");
|
// String user = (String)config.get("user");
|
||||||
String authType = (String)config.get("authType");
|
// String authType = (String)config.get("authType");
|
||||||
String authPass = (String)config.get("authPass");
|
// String authPass = (String)config.get("authPass");
|
||||||
String privType = (String)config.get("privType");
|
// String privType = (String)config.get("privType");
|
||||||
String privPass = (String)config.get("privPass");
|
// String privPass = (String)config.get("privPass");
|
||||||
String[] oids = (String[])config.get("oids");
|
// String[] oids = (String[])config.get("oids");
|
||||||
String method = (String)config.get("method");
|
// String method = (String)config.get("method");
|
||||||
|
//
|
||||||
switch (method) {
|
// switch (method) {
|
||||||
case "validate":
|
// case "validate":
|
||||||
return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
|
// return snmpV3.validate(ip, port, user, authType, authPass, privType, privPass);
|
||||||
case "get":
|
// case "get":
|
||||||
return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
|
// return snmpV3.get(ip, port, user, authType, authPass, privType, privPass, oids);
|
||||||
case "walk":
|
// case "walk":
|
||||||
return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
|
// return snmpV3.walk(ip, port, user, authType, authPass, privType, privPass, oids[0]);
|
||||||
default:
|
// default:
|
||||||
new Exception("Unknown SNMP command : " + method).printStackTrace();
|
// new Exception("Unknown SNMP command : " + method).printStackTrace();
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
} catch (Exception e) {
|
// } catch (Exception e) {
|
||||||
new Exception("An Exception happend : " + e.getMessage()).printStackTrace();
|
// new Exception("An Exception happend : " + e.getMessage()).printStackTrace();
|
||||||
} finally {
|
// } finally {
|
||||||
if (snmp != null) {
|
// if (snmp != null) {
|
||||||
try {
|
// try {
|
||||||
snmp.close();
|
// snmp.close();
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
if (transport != null) {
|
// if (transport != null) {
|
||||||
try {
|
// try {
|
||||||
transport.close();
|
// transport.close();
|
||||||
} catch (IOException e) {
|
// } catch (IOException e) {
|
||||||
e.printStackTrace();
|
// e.printStackTrace();
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -46,13 +46,13 @@ public class AppTest {
|
||||||
config.put("method", "walk");
|
config.put("method", "walk");
|
||||||
config.put("oids", new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
|
config.put("oids", new String[]{"1.3.6.1.2.1.25.2.3.1.3"});
|
||||||
|
|
||||||
Object result = c.getInternal(config);
|
// Object result = c.getInternal(config);
|
||||||
|
//
|
||||||
if(result instanceof Boolean) {
|
// if(result instanceof Boolean) {
|
||||||
System.out.println("validate : " + result);
|
// System.out.println("validate : " + result);
|
||||||
}else {
|
// }else {
|
||||||
printResult((Map<String, String>)result);
|
// printResult((Map<String, String>)result);
|
||||||
}
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user