snmp4j 介绍
?
import java.io.IOException;import org.snmp4j.*;import org.snmp4j.event.ResponseEvent;import org.snmp4j.event.ResponseListener;import org.snmp4j.mp.MPv3;import org.snmp4j.mp.SnmpConstants;import org.snmp4j.security.*;import org.snmp4j.smi.Address;import org.snmp4j.smi.GenericAddress;import org.snmp4j.smi.OID;import org.snmp4j.smi.OctetString;import org.snmp4j.smi.UdpAddress;import org.snmp4j.smi.VariableBinding;import org.snmp4j.transport.DefaultUdpTransportMapping;public class Snmp_manager {private Snmp snmp = null;private String version = null;/** * * @param version */public Snmp_manager(String version) {try {this.version = version;TransportMapping transport = new DefaultUdpTransportMapping();snmp = new Snmp(transport);if (version.equals("3")) {// 设置安全模式USM usm = new USM(SecurityProtocols.getInstance(),new OctetString(MPv3.createLocalEngineID()), 0);SecurityModels.getInstance().addSecurityModel(usm);}// 开始监听消息transport.listen();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * * @param syn * 是否是同步模式 * @param bro * 是否是广播 * @param pdu * 要发送的报文 * @param addr * 目标地址 * @throws IOException */public void sendMessage(Boolean syn, final Boolean bro, PDU pdu, String addr)throws IOException {// 生成目标地址对象Address targetAddress = GenericAddress.parse(addr);Target target = null;if (version.equals("3")) {// 添加用户snmp.getUSM().addUser(new OctetString("MD5DES"),new UsmUser(new OctetString("MD5DES"), AuthMD5.ID,new OctetString("MD5DESUserAuthPassword"),PrivDES.ID, new OctetString("MD5DESUserPrivPassword")));target = new UserTarget();// 设置安全级别((UserTarget) target).setSecurityLevel(SecurityLevel.AUTH_PRIV);((UserTarget) target).setSecurityName(new OctetString("MD5DES"));target.setVersion(SnmpConstants.version3);} else {target = new CommunityTarget();if (version.equals("1")) {target.setVersion(SnmpConstants.version1);((CommunityTarget) target).setCommunity(new OctetString("public"));} else {target.setVersion(SnmpConstants.version2c);((CommunityTarget) target).setCommunity(new OctetString("public"));}}// 目标对象相关设置target.setAddress(targetAddress);target.setRetries(5);target.setTimeout(1000);if (syn.equals(true)) {// 发送报文 并且接受响应ResponseEvent response = snmp.send(pdu, target);// 处理响应System.out.println("Synchronize message from "+ response.getPeerAddress() + "/nrequest:"+ response.getRequest() + "/nresponse:"+ response.getResponse());} else {// 设置监听对象ResponseListener listener = new ResponseListener() {@Overridepublic void onResponse(ResponseEvent event) {// TODO Auto-generated method stubif (bro.equals(false)) {((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);}};// 发送报文snmp.send(pdu, target, null, listener);}}public static void main(String[] args) {Snmp_manager manager = new Snmp_manager("2c");// 构造报文PDU pdu = new PDU();// PDU pdu = new ScopedPDU();// 设置要获取的对象IDOID oids = new OID("1.3.6.1.2.1.1.1.0");pdu.add(new VariableBinding(oids));// 设置报文类型pdu.setType(PDU.GETNEXT);// ((ScopedPDU) pdu).setContextName(new OctetString("priv"));try {// 发送消息 其中最后一个是想要发送的目标地址manager.sendMessage(false, true, pdu, "udp:127.0.0.1/161");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}??