java用com.adventnet包读mib

public static boolean readMibWalk(String ip, String community, SnmpOID[] oids, Vector[] resultList, Vector indexList) {
        SnmpAPI snmpapi = null;
        SnmpSession session = null;
        try {
            snmpapi = new SnmpAPI();
            snmpapi.start();
            session = new SnmpSession(snmpapi);
            session.open();

            SnmpPDU pdu = new SnmpPDU();
            pdu.setRemoteHost(ip); //OLT上的ip
            pdu.setRemotePort(161);
            pdu.setCommunity(community);
            pdu.setTimeout(5000);
            pdu.setRetries(2);
            pdu.setVersion(SnmpAPI.SNMP_VERSION_2C);
            pdu.setCommand(SnmpAPI.GETNEXT_REQ_MSG);
            for (int i = 0; i < oids.length; i++) { //第几个列(也就是第几个参数) ,i是列号
                pdu.addNull(oids[i]);
                resultList[i] = new Vector();
                while (true) {   //循环 ,读取每一行
                    SnmpPDU v = session.syncSend(pdu);  //本次读回来得到的内容,下面是解析了
                    if (v == null) {
                        return false;
                    }
                    if (isInSubTree(oids[i].toIntArray(), v.getObjectID(0))) { //通过列的oid,去判断读的是不是还是这个列
                        String t = v.getVariableBinding(0).toString(); //索引+ 值
                        int index = t.indexOf(":");
                        resultList[i].add(t.substring(index + 2)); //存值
                        pdu.removeVariableBinding(0);  //移除上一个行号
                        pdu.addNull(v.getObjectID(0)); //绑定当前行号,可以去读下一行
                        if (i == 0) {   //读第一列的时候,记录索引号
                            String d = getIndexs(oids[i].toIntArray(), v.getObjectID(0)); //得到索引值
                            indexList.add(d);
                        }
                    } else {
                        break;
                    }
                }
                pdu.removeVariableBinding(0);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (null != session) {
                snmpapi.close();
                session.close();
                session = null;
            }
        }
        return true;
    }

 

public static boolean readMibBulk(String ip, String community, SnmpOID[] oids, Vector[] resultList, Vector indexList) {
        SnmpAPI snmpapi = null;
        SnmpSession session = null;
        int maxRepInt = 25;  //设置每次读的行数
        try {
            snmpapi = new SnmpAPI();
            snmpapi.start();
            session = new SnmpSession(snmpapi);
            session.open();

            SnmpPDU pdu = new SnmpPDU();
            pdu.setRemoteHost(ip);
            pdu.setRemotePort(161);
            pdu.setCommunity(community);
            pdu.setTimeout(5000);
            pdu.setRetries(2);
            pdu.setVersion(SnmpAPI.SNMP_VERSION_2C);
            pdu.setCommand(SnmpAPI.GETBULK_REQ_MSG);
            pdu.setMaxRepetitions(maxRepInt);
            pdu.setNonRepeaters(0);

            for (int i = 0; i < oids.length; i++) {
                pdu.addNull(oids[i]);
                resultList[i] = new Vector();
                while (true) {
                    SnmpPDU v = session.syncSend(pdu);
                    if (v == null) {
                        return false;
                    }
                    int j = 0;
                    SnmpOID loid = null;
                    for (j = 0; j < maxRepInt; j++) {  //再加了一个循环,循环这25行内是否有异常数据
                        if (isInSubTree(oids[i].toIntArray(), v.getObjectID(j))) {
                            String t = v.getVariableBinding(j).toString();
                            int index = t.indexOf(":");
                            resultList[i].add(t.substring(index + 2));
                            pdu.removeVariableBinding(j);
                            loid = v.getObjectID(j); //记录当前行号
                            String d = getIndexs(oids[i].toIntArray(), v.getObjectID(j));
                            indexList.add(d); //一直记录索引号,其实可以加上 if (i == 0)语句的
                        } else {
                            break; //如果读回来的25行中,第几行不是了,那么跳出循环
                        }
                    }
                    //是否是读完25行,没有跳出
                    if (j == maxRepInt) { //是,那么去读下一个25行,从loid行开始
                        pdu.addNull(loid);
                    } else {
                        break; //不是,有异常数据,接着跳出while循环
                    }
                }
                pdu.removeVariableBinding(0); // 读取下一列
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (null != session) {
                snmpapi.close();
                session.close();
                session = null;
            }
        }
        return true;

    }

 

public static boolean isInSubTree(int oids[], SnmpOID snmpoid) { //oid设定的oid值,snmpoid为当前读取的oid值
        if (snmpoid == null) {
            return false;
        }
        int ai1[] = (int[]) snmpoid.toValue();
        if (ai1 == null) {
            return false;
        }
        int i = oids.length;
        if (ai1.length < i) {
            return false;
        }
        for (int j = i - 1; j >= 0; j--) {
            if (ai1[j] != oids[j]) {
                return false;
            }
        }
        return true;
    }

    public static String getIndexs(int[] oids, SnmpOID snmpoid) {
        String ret = "";
        if (snmpoid == null) {
            return "";
        }
        int ai1[] = (int[]) snmpoid.toValue();
        if (ai1 == null) {
            return "";
        }
        int i = oids.length;
        if (ai1.length < i) {
            return "";
        }
        for (int j = i - 1; j >= 0; j--) {
            if (ai1[j] != oids[j]) {
                return "";
            }
        }
        for (int k = i; k < ai1.length; k++) {
            ret = ret + ai1[k];
            ret = ret + ".";
        }
        return ret;
    }

 

你可能感兴趣的:(java,J#)