`

用com.adventnet.snmp包读mib

    博客分类:
  • SNMP
 
阅读更多
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 MIB 查看器 Adventnet MibBrower

    MibBrower SNMP采数 Mib查看器 Adventnet MibBrower

    AdventNet SNMP Utilities 5

    测试snmp mib时所用软件,支持IPv6 mib获取

    AdventNet SNMP Agent for Linux

    AdventNet SNMP Agent for Linux能简化网络中Linux机器的监控和管理,支持名为AdventNet-LINUX-MIB的预定义MIB,以及诸如MIB RFC-1213-MIB和HOST-RESOURCES-MIB的标准MIB。此外,它使用SNMP和HTTP控制台以便于管理。...

    SNMP MIB 查看器 AdventNet MIB Browser

    AdventNet MIB Browser是一个便于用户使用的工具,用来测试和监管网络上的多个SNMP设备。它允许网络和系统工程师加载标准的或某些供应商专有的MIB,并通过设备上运行的SNMP代理检索有关软件和硬件配置的数据。

    基于SNMP/MIB的网络数据获取系统设计与实现

    AdventNet SNMP API为基于SNMP的网络管理应用提供了一个全面的开发工具包。AdventNet的SNMP栈包含一系列强大的Java SNMP库,用来为监控和跟踪网络元素创建实时的应用程序,这些应用程序是可靠的、可伸展的且独立于OS...

    基于SNMP/MIB的网络数据获取系统设计与实现

    java实现 带完整源代码和文档 基于SNMP/MIB的网络数据获取系统设计与实现 基于AdventNet SNMP API 这是我以前做的课程,现在翻出来,拿出来跟大家共享!

    一个VC写的mib browser

    这是2001年我用vc6.0写的一个mib浏览器的源码,运行时先打开目录下的RFC1155-SMI.txt,然后打开RFC1213-MIB.txt,然后再加载你自己的mib. 界面是仿制adventnet的mib browser风格 我已经很多年不用vc,也有很长...

    用snmp获取网络设备的指标信息

    利用Adventnet SNMP API的MibTree等类,装载MIB模型,显示MIB树

    AdventNet_Agent_Toolkit_Java_Edition_6_0_0

    用于SNMP协议开发时用的 可编辑代理端 查看mib库等

    delphi写的mib browser 源码

    2001年时我用delphi写的mib browser源码,比那个用vc写的好用的多,界面也仿adventnet的。 zz1976@163.com

    监视通信线路,路由器或交换机SNMP trap的配置

    本实验,首先在网络设备方配置好SNMP trap,然后分别用Ethereal、AdventNet MibBrowser 的 Trap Viewer 和一个自己开发的网管软件,来捕获分析trap,从而达到监视通信线路的目的。 物理通信线路的正常工作是一切网络...

    SNMP API for dotNET 4.0

    用它来编写一个网管软件或者MIB编译器、浏览器不费吹灰之力,推荐给大家!是Adventnet公司的作品,我这里仅作收藏,帮助文档可以到官方网站上查看。欢迎大家下载,使用,讨论。建议大家使用.net框架开发涉及到网络、...

Global site tag (gtag.js) - Google Analytics