相比SNMP PACKAGE API 比较低的门槛和简单的功能,SNMP4J则是完全专业级的框架.他的SNMP-TARGET-PDU的三点结构更科学更严谨,完善的PDU数据引擎也是业界的一种潮流.SNMP4J的强大体现在他对SNMPV3标准下基于用户安全(USM)方式的认证,加密,和对Agent功能的强大支持.
public class Snmp4JHelper { public static Snmp createSnmpSession(Address address) throws IOException { // new TransportMapping object AbstractTransportMapping transport; if (address instanceof TcpAddress) { transport = new DefaultTcpTransportMapping(); } else { transport = new DefaultUdpTransportMapping(); } // new Snmp object Snmp snmp = new Snmp(transport);
return snmp; }
public static Target createTarget(Address address,OctetString community) { CommunityTarget target = new CommunityTarget(); target.setAddress(address); target.setCommunity(community); return target;
}
public static PDU createPDU(int pduType) { PDU request;
if (pduType == PDU.V1TRAP) { request = new PDUv1(); } else { request = new PDU(); } request.setType(pduType); return request; }
几个工具方法
public static OctetString createOctetString(String s) { OctetString octetString; if (s.startsWith("0x")) { octetString = OctetString.fromHexString(s.substring(2), ':'); } else { octetString = new OctetString(s); } return octetString; }
public static Address getAddress(String transportAddress) { // 如果有冒号存在,就是"udp:xxx.xxx.xxx.xxx"的格式,分离出协议和地址 int colon = transportAddress.indexOf(':'); String transprotocol = "udp"; if (colon > 0) { transprotocol = transportAddress.substring(0, colon); transportAddress = transportAddress.substring(colon+1); } // append default port follow end of transportAddress if (transportAddress.indexOf('/') < 0) { transportAddress += "/161"; } if (transprotocol.equalsIgnoreCase("udp")) { return new UdpAddress(transportAddress); } else if (transprotocol.equalsIgnoreCase("tcp")) { return new TcpAddress(transportAddress); } throw new IllegalArgumentException("Unknown transprotocol "+transprotocol); }
2.定义Snmp Engine,负责维护基本的Snmp连接
public class SnmpV1V2cEngine {
private Snmp snmp; private Target target;
private Address address; private OctetString community = new OctetString("public"); private int version = SnmpConstants.version2c;
private int retries = 1; private int timeout = 1000; private int maxSizeResponsePDU = 65535;
public SnmpV1V2cEngine(String address, String community) { this(address, community,null); }
if (respEvent.getResponse() == null) { // request timed out System.out.println("response is null"); }else { VariableBinding tempV = null; Vector<VariableBinding> responseVB = respEvent.getResponse().getVariableBindings(); if(1 == responseVB.size()){ tempV = responseVB.elementAt(0); // snmp request获取的结果,再赋值给currentOid,形成类似递归的效果 currentOid = tempV.getOid().toString(); String value = tempV.getVariable().toString().trim(); // get ifindex from oid // get the last word after a "." if (currentOid.startsWith(baseOid)) { // extract interface number from oid int lastDot = currentOid.lastIndexOf("."); String indexStr = currentOid.substring(lastDot + 1); int index = Integer.parseInt(indexStr); // store interface description map.put(new Integer(index), value); } else { // 如果获取的oid不再startsWith baseOid,就终止循环 break; } } // end of if }// end of if } // end of while
==================get测试===================== Received response from: 192.168.0.100/161 1.3.6.1.2.1.1.1.0:Hardware: x86 Family 15 Model 2 Stepping 9 AT/AT COMPATIBLE - Software: Windows 2000 Version 5.1 (Build 2600 Uniprocessor Free) ==================getNext测试================= 共5个接口 MS TCP Loopback interface VMware Virtual Ethernet Adapter for VMnet8 VMware Virtual Ethernet Adapter for VMnet1 Realtek RTL8139 Family PCI Fast Ethernet NIC - 数据包计划程序微型端口 ASUS 802.11b/g Wireless LAN Card - 数据包计划程序微型端口