用com.adventnet.snmp包读mib

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) {   //当读该参数的25行后还没有读完,则继续读25行
                    SnmpPDU v = session.syncSend(pdu);
                    if (v == null) {
                        return false;
                    }
                    int j = 0;
                    SnmpOID loid = null;
                    for (j = 0; j < maxRepInt; j++) {//读每一列
                        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);
                        } else {
                            break;  //当块读的参数不符合的时候
                        }
                    }
                    //读完一列后
                    if (j == maxRepInt) {
                        pdu.addNull(loid);  //当读了25行,内容还没有读完,则从loid行开始继续读25行
                    } 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 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);
            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++) { //读一个参数
                pdu.addNull(oids[i]);
                resultList[i] = new Vector();
                while (true) {    //用get-next的方式一行一行的读取
                    SnmpPDU v = session.syncSend(pdu);
                    if (v == null) {
                        return false;
                    }
                    if (isInSubTree(oids[i].toIntArray(), v.getObjectID(0))) {
                        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 readMibCurrent(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);
            pdu.setRemotePort(161);
            pdu.setCommunity(community);
            pdu.setTimeout(5000);
            pdu.setRetries(2);
            pdu.setVersion(SnmpAPI.SNMP_VERSION_2C);
            pdu.setCommand(SnmpAPI.GET_REQ_MSG);
            //两重循环
            for (int i = 0; i < oids.length; i++) { //读一个参数
                pdu.addNull(oids[i]);
                resultList[i] = new Vector();
               
                SnmpPDU v = session.syncSend(pdu);
                if (v == null) {
                    return false;
                }
                if (isInSubTree(oids[i].toIntArray(), v.getObjectID(0))) {
                    String t = v.getVariableBinding(0).toString();
                    int index = t.indexOf(":");
                    resultList[i].add(t.substring(index + 2)); //返回值
                    pdu.removeVariableBinding(0);
                }

               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 readMibCurrent(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);
            pdu.setRemotePort(161);
            pdu.setCommunity(community);
            pdu.setTimeout(5000);
            pdu.setRetries(2);
            pdu.setVersion(SnmpAPI.SNMP_VERSION_2C);
            pdu.setCommand(SnmpAPI.GET_REQ_MSG);
            //两重循环
            for (int i = 0; i < oids.length; i++) { //读一个参数
                resultList[i] = new Vector();
                pdu.addNull(oids[i]);
            }

            SnmpPDU v = session.syncSend(pdu);
            if (v == null) {
                return false;
            }

            for(int j = 0; j < oids.length;j++){
                if (isInSubTree(oids[j].toIntArray(), v.getObjectID(j))) {
                    String t = v.getVariableBinding(j).toString();
                    int index = t.indexOf(":");
                    resultList[j].add(t.substring(index + 2)); //返回值
                    pdu.removeVariableBinding(j);
                }
            }

           pdu.removeVariableBinding(0);
            
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            if (null != session) {
                snmpapi.close();
                session.close();
                session = null;
            }
        }
        return true;
    }

 

private void setMib(String host, int port, String community,
                            String writeCommunity, String oid, String value) throws
                SnmpException {
            SnmpAPI snmpapi = new SnmpAPI();
            snmpapi.start();
            SnmpSession session = new SnmpSession(snmpapi);
            session.open();
            SnmpPDU pdu = new SnmpPDU();
            try {
                pdu.setRemoteHost(host);
                pdu.setRemotePort(port);
                pdu.setCommunity(community);
                pdu.setWriteCommunity(writeCommunity);
                pdu.setTimeout(5000);
                pdu.setRetries(3);
                pdu.setVersion(SnmpAPI.SNMP_VERSION_2C);
                pdu.setCommand(SnmpAPI.SET_REQ_MSG);

                SnmpOID snmpOID = new SnmpOID(oid); // add OID
                // create SnmpVar instance for the value and the type
                SnmpVar var = null;
                try {
                    var = SnmpVar.createVariable(value, SnmpAPI.INTEGER); //设置下发配置的值和类型
                } catch (SnmpException e) {
                    System.err.println("Cannot create variable: " + oid +
                                       " with value:1");
                    return;
                }
                SnmpVarBind varbind = new SnmpVarBind(snmpOID, var); //create varbind
                pdu.addVariableBinding(varbind); // add variable binding
                // Send PDU
                try {
                    pdu = session.syncSend(pdu);
                } catch (SnmpException e) {
                    System.err.println("Sending PDU" + e.getMessage());
                }
                if (pdu == null) {
                    System.out.println("Request timed out!");
                    return;
                }
                System.out.println("Response PDU received from " +
                                   pdu.getAddress() + ", community: " +
                                   pdu.getCommunity());
                if (pdu.getErrstat() != 0) {
                    System.err.println(pdu.getError());
                } else {
                    System.out.println(pdu.printVarBinds());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (session != null) {
                    snmpapi.close();
                    session.close();
                    session = null;
                }
            }
        }
    }

 

你可能感兴趣的:(snmp)