相比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")) { retur