不知道有多少人用过snmp4j,最近在用这东西,想问个问题:比如说一个交换机,读取交换机的MIB库,只要将OID与请求包进行绑定,而后通过应该包就能读取出对应OID的值,我想问的是,对于MIB库中的表结构数据怎么样进行读取,比如说如何读取交换机各个接口的MAC地址?        我是这么做的:ifPhysAddress的OID1.3.6.1.2.1.2.2.1.6,然后在该OID后面加上“.端口号”就是是表示特定接口的OID,然后进行绑定,但是结果不正确,读取到的MAC地址都跟第一次绑定的接口OID相同        知道的麻烦给我个思路,万分感谢

解决方案 »

  1.   

    我算是自己搞清楚,但是又出现新问题,MAC表是怎么读取的?
      

  2.   

    snmp4j略知一直,可以用PDUFactory 和TableUtils 来实现,上个代码给你看看,获取交换机的MAC表的
    public Map<Integer,String> getPortAFT(){
    Map<Integer,String> AFT=null;
    ArrayList<String> macSet=new ArrayList<String>();
    int maxRepetitions = 100;
    Snmp protocol=null;
    CommunityTarget target=null;
    Address targetAddress=null;
    TransportMapping transportProtocol=null;
    target=new CommunityTarget();
    targetAddress=GenericAddress.parse("udp:"+this.switchIP+"/161");
    target.setAddress(targetAddress);
    target.setCommunity(new OctetString("public"));
    target.setRetries(2);
    target.setTimeout(5000);
    target.setVersion(SnmpConstants.version2c);
    try {
    transportProtocol=new DefaultUdpTransportMapping();
    transportProtocol.listen();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    protocol=new Snmp(transportProtocol);
    try {
    protocol.listen();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    PDUFactory pF =new DefaultPDUFactory (PDU.GETNEXT);
            TableUtils tableUtils =new TableUtils(protocol, pF);
            tableUtils.setMaxNumRowsPerPDU(maxRepetitions);
            OID[] columns =new OID[1];
            columns[0] =new OID("1.3.6.1.2.1.17.4.3.1.1" ); 
            
            NetTools tools=new NetTools();
            String MACAddress=getLastInterfaceMAC();
            String decimalMAC=tools.decimalMAC(MACAddress);
            
            OID lowerBoundIndex =  new OID(decimalMAC);
            OID upperBoundIndex =  null;
            List AFTList =  tableUtils.getTable(target, columns, lowerBoundIndex, upperBoundIndex);
            
            for ( int j = 0; j < AFTList.size()-1;j++){
             String containVbs=null;
             int indexVbs=AFTList.get(j).toString().indexOf("vbs");
             containVbs=AFTList.get(j).toString().substring(indexVbs);
            
             String containFirstEqualSign=null;
             int indexFirstEqualSign=containVbs.indexOf("=");
             containFirstEqualSign=containVbs.substring(indexFirstEqualSign+2);
            
             String mac=null;
             int  indexSecondEqualSign=containFirstEqualSign.indexOf("=");
             int indexOfEnd=containFirstEqualSign.indexOf("]");
             mac=containFirstEqualSign.substring(indexSecondEqualSign+2,indexOfEnd);
             System.out.println(mac);
             macSet.add(mac);
            
                
            }
            return AFT;
    }