标签:
class SnmpManager { private TransportMapping transportMapping = null; private Snmp snmp = null; private int version; public final static int version1 = SnmpConstants. version1; public final static int version2c = SnmpConstants.version2c; public final static int version3 = SnmpConstants. version3; /** * 构造方法 * @param version */ public SnmpManager( int version) { this. version = version; try { // 设置成Udp协议 transportMapping = new DefaultUdpTransportMapping(); snmp = new Snmp( transportMapping); if (version == version3) { // 设置安全信息 USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels. getInstance().addSecurityModel(usm); } transportMapping.listen(); } catch (Exception e) { e.printStackTrace(); System. out.println(e); } } /** * @param sync * @param bro * @param pdu * @param addr * 发送消息方法 */ public void sendMsg(boolean sync, final boolean bro, PDU pdu, String addr) { Address targetAddres = GenericAddress. parse(addr); Target target = null; if ( this. version == version3) { snmp.getUSM().addUser( new OctetString( "MD5DES"), new UsmUser( new OctetString( "MD5DES"), AuthMD5. ID, new OctetString("MD5DESUserAuthPassword" ), PrivDES.ID, new OctetString("MD5DESUserPrivPassword" ))); target = new UserTarget(); // 设置安全级别 target.setSecurityLevel(SecurityLevel. AUTH_PRIV); target.setSecurityName( new OctetString("MD5DES")); target.setVersion(SnmpConstants. version3); } else { target = new CommunityTarget(); if ( this. version == version1) { target.setVersion( version1); ((CommunityTarget) target).setCommunity(new OctetString("public" )); } else { target.setVersion( version2c); ((CommunityTarget) target).setCommunity(new OctetString("public" )); } } target.setAddress(targetAddres); target.setRetries(2); target.setTimeout(1000); if (sync) { // 发送报文 并且接受响应 ResponseEvent response = null; try { response = snmp.send(pdu, target); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System. out.println(e); } // 处理响应 System. out.println( "Synchronize message from " + response.getPeerAddress() + "/nrequest:" + response.getRequest() + "/nresponse:" + response.getResponse()); } else { ResponseListener listener = new ResponseListener() { @Override public void onResponse(ResponseEvent event) { if (!bro) { ((Snmp) event.getSource()).cancel(event.getRequest(), this ); } // 处理响应 PDU request = event.getRequest(); PDU response = event.getResponse(); System. out.println( "Asynchronise message from " + event.getPeerAddress() + "/nrequest:" + request + "/nresponse:" + response); } }; try { snmp.send(pdu, target, null, listener); } catch (IOException e) { e.printStackTrace(); System. out.println(e); } } } } public class SnmpTest { public static String myVersion = ""; static boolean sync = false; static boolean bro = false; /** * 主函数 * @param args */ public static void main(String[] args) { SnmpManager manager = new SnmpManager(SnmpConstants.version2c ); // 构造报文 PDU pdu = new PDU(); // PDU pdu = new ScopedPDU(); version3使用 // 设置要获取的对象ID OID oids = new OID( "1.3.6.1.2.1.1.1.0"); // OID oids = new OID(new int [] { 1, 3, 6, 1, 2, 1, 1, 1, 0 }); pdu.add( new VariableBinding(oids)); // 设置报文类型 pdu.setType(PDU. GET); // ((ScopedPDU) pdu).setContextName(new OctetString("priv")); // 发送消息 其中最后一个是想要发送的目标地址 manager.sendMsg( true, true, pdu, "udp:127.0.0.1/161"); } }
标签:
原文地址:http://blog.csdn.net/leirenbaobao/article/details/43647025