• snmp4j 过滤错误的节点


    1.主要代码如下: 开发包(snmp4j.jar)

    // 省略了导入的包
    public
    class SnmpTest1 { public static void main(String[] args) { try { Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); snmp.listen(); CommunityTarget target = new CommunityTarget(); target.setAddress(GenericAddress.parse("udp:192.168.100.61/161")); target.setCommunity(new OctetString("public")); target.setTimeout(1000); target.setRetries(2); target.setVersion(SnmpConstants.version1); PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.2.0"))); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.1"))); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.2"))); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.3"))); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.3.4"))); pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.1.4.0"))); PDU newpdu = getSucPDU(snmp,target,pdu); ResponseEvent event = snmp.get(newpdu, target); PDU resp = event.getResponse(); for (int i = 0; i < resp.size(); i++) { VariableBinding vb = resp.get(i); System.out.println(vb.getOid().toString() +"^^^^"+ vb.getVariable()); } } catch (IOException e) { e.printStackTrace(); } } private static PDU getSucPDU(Snmp snmp, CommunityTarget target, PDU pdu) throws IOException { ResponseEvent event = snmp.get(pdu, target); PDU resp = event.getResponse(); int errorIndex = resp.getErrorIndex(); if(errorIndex != 0){ VariableBinding vb = resp.get(errorIndex -1); OID errorOid = vb.getOid(); System.out.println(errorOid + ":" + resp.getErrorStatusText()); pdu.remove(errorIndex-1); getSucPDU(snmp,target,pdu); return pdu; } return null; } }

    2. 运行结果为:
     
  • 相关阅读:
    给定一个排序数组,你需要在原地删除重复出现的元素
    OSPF-外部路由
    虚链路
    OSPF域间路由计算,防环
    转 C# 只允许运行一个实例
    转 点击关闭时最小化到任务栏
    C#,int转成string,string转成int
    SQL 查找表名 字段名
    C# *= 运算顺序
    SQL 批量删除表
  • 原文地址:https://www.cnblogs.com/beibidewomen/p/7217992.html
Copyright © 2020-2023  润新知