snmp4j v3

来源:互联网 发布:crm软件排行 编辑:程序博客网 时间:2024/05/17 04:15
import java.io.IOException;import java.util.List;import java.util.Vector;import org.snmp4j.PDU;import org.snmp4j.ScopedPDU;import org.snmp4j.Snmp;import org.snmp4j.Target;import org.snmp4j.UserTarget;import org.snmp4j.event.ResponseEvent;import org.snmp4j.mp.MPv3;import org.snmp4j.mp.SnmpConstants;import org.snmp4j.security.AuthMD5;import org.snmp4j.security.PrivDES;import org.snmp4j.security.SecurityLevel;import org.snmp4j.security.SecurityModels;import org.snmp4j.security.SecurityProtocols;import org.snmp4j.security.USM;import org.snmp4j.security.UsmUser;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;import org.snmp4j.util.DefaultPDUFactory;import org.snmp4j.util.TableEvent;import org.snmp4j.util.TableUtils;@SuppressWarnings("unused")public class SnmpUtilSendTrap {public static void main(String[] args) throws IOException, InterruptedException {Snmp snmp = new Snmp(new DefaultUdpTransportMapping());USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);SecurityModels.getInstance().addSecurityModel(usm);snmp.listen();// Add UserUsmUser user = new UsmUser(new OctetString("myv3"),AuthMD5.ID, new OctetString("authpass123"),null, null);//If the specified SNMP engine id is specified, this user can only be used with the specified engine ID//So if it's not correct, will get an error that can't find a user from the user table.//snmp.getUSM().addUser(new OctetString("nmsAdmin"), new OctetString("0002651100"), user);snmp.getUSM().addUser(new OctetString("myv3"), user);UserTarget target = new UserTarget();target.setVersion(SnmpConstants.version3);target.setAddress(new UdpAddress("172.28.140.70/161"));target.setSecurityLevel(SecurityLevel.AUTH_NOPRIV);target.setSecurityName(null);//new OctetString("myv3")target.setTimeout(3000);//3starget.setRetries(0);//OctetString contextEngineId = new OctetString("8000007701ac1c8c12");//("0002651100[02]");OctetString contextEngineId = getContextEngineId();//contextEngineId = new OctetString("0002651100[02]");//("0002651100[02]");sendRequest(snmp, createGetPdu(contextEngineId), target);//snmpWalk(snmp, target, contextEngineId);}private static  OctetString getContextEngineId() throws IOException {// TODO 自動生成されたメソッド・スタブSnmp snmp = new Snmp(new DefaultUdpTransportMapping());USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);SecurityModels.getInstance().addSecurityModel(usm);snmp.listen();// Add UserUsmUser user = new UsmUser(new OctetString("myv3"),AuthMD5.ID, new OctetString("authpass123"),null, null);//If the specified SNMP engine id is specified, this user can only be used with the specified engine ID//So if it's not correct, will get an error that can't find a user from the user table.//snmp.getUSM().addUser(new OctetString("nmsAdmin"), new OctetString("0002651100"), user);//snmp.getUSM().addUser(null, null);snmp.getUSM().addUser(new OctetString("myv3"), user);UserTarget target = new UserTarget();target.setVersion(SnmpConstants.version3);target.setAddress(new UdpAddress("172.28.140.70/161"));target.setSecurityLevel(SecurityLevel.NOAUTH_NOPRIV);target.setSecurityName(new OctetString("myv3"));target.setTimeout(3000);//3starget.setRetries(0);ScopedPDU pdu = new ScopedPDU();pdu.setType(PDU.GET);//pdu.setContextEngineID(contextEngineId);//if not set, will be SNMP engine id//pdu.setContextName(contextName);  //must be same as SNMP agent//pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0")));//sysUpTimepdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));//sysNameResponseEvent responseEvent = snmp.send(pdu, target);OctetString contextEngineId = ((ScopedPDU) responseEvent.getResponse()).getContextEngineID();return contextEngineId;}private static PDU createGetPdu(OctetString contextEngineId) {ScopedPDU pdu = new ScopedPDU();pdu.setType(PDU.GET);pdu.setContextEngineID(contextEngineId);//if not set, will be SNMP engine id//pdu.setContextName(contextName);  //must be same as SNMP agent//pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.0")));//sysUpTimepdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5.0")));//sysName//pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.5")));//expect an no_such_instance errorreturn pdu;}private static void sendRequest(Snmp snmp, PDU pdu, UserTarget target)throws IOException {ResponseEvent responseEvent = snmp.send(pdu, target);PDU response = responseEvent.getResponse();OctetString contextEngineId = ((ScopedPDU) responseEvent.getResponse()).getContextEngineID();if (response == null) {System.out.println("TimeOut...");} else {//response.getResponse();if (response.getErrorStatus() == PDU.noError) {Vector<? extends VariableBinding> vbs = response.getVariableBindings();for (VariableBinding vb : vbs) {//System.out.println(contextEngineId);System.out.println(vb + " ," + vb.getVariable().getSyntaxString());}} else {System.out.println("Error:" + response.getErrorStatusText());}}}private static void snmpWalk(Snmp snmp, UserTarget target, OctetString contextEngineId) {TableUtils utils = new TableUtils(snmp,new MyDefaultPDUFactory(PDU.GETNEXT, //GETNEXT or GETBULK)contextEngineId));utils.setMaxNumRowsPerPDU(5);//only for GETBULK, set max-repetitions, default is 10OID[] columnOids = new OID[] {new OID("1.3.6.1.2.1.1.9.1.2"),//sysORIDnew OID("1.3.6.1.2.1.1.9.1.3")//sysORDescr};// If not null, all returned rows have an index in a range (lowerBoundIndex, upperBoundIndex]List<TableEvent> l = utils.getTable(target, columnOids, new OID("3"), new OID("10"));for (TableEvent e : l) {System.out.println(e);}}private static class MyDefaultPDUFactory extends DefaultPDUFactory {private OctetString contextEngineId = null;public MyDefaultPDUFactory(int pduType, OctetString contextEngineId) {super(pduType);this.contextEngineId = contextEngineId;}@Overridepublic PDU createPDU(Target target) {PDU pdu = super.createPDU(target);if (target.getVersion() == SnmpConstants.version3) {((ScopedPDU)pdu).setContextEngineID(contextEngineId);}return pdu;}}}