【问题标题】:net-snmp sample code to parse MIB file and extract trap related information from it用于解析 MIB 文件并从中提取陷阱相关信息的 net-snmp 示例代码
【发布时间】:2011-11-11 21:51:05
【问题描述】:

我在 Windows 上使用 net-snmp C 库。我想从 MIB 文件中解析陷阱相关信息。

我需要一些示例代码来执行此操作。我在http://www.net-snmp.org/ 上没有发现任何有用的信息

【问题讨论】:

    标签: c++ windows snmp mib snmp-trap


    【解决方案1】:

    这里是一些使用 net-snmp 库解析 MIB 文件的示例代码。 在使用此代码之前,您需要在项目属性中引用或添加 net-snmp 的 Include 和 Lib 目录:

    #include "stdafx.h"
    #include <net-snmp/net-snmp-config.h>
    #include <net-snmp/net-snmp-includes.h>
    #include <net-snmp/definitions.h>
    #include <net-snmp/library/tools.h>
    #include <net-snmp/mib_api.h>
    #include <net-snmp/library/mib.h>
    #include <net-snmp/library/parse.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        struct tree *tp;
        struct tree *tree_head = NULL ;
        FILE *fp = NULL;
    
        char str[] = "c:\\MyMIBFileDir\\My.mib";
    
        netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_MIB_WARNINGS , 2);
        netsnmp_ds_toggle_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SAVE_MIB_DESCRS);
    
        netsnmp_init_mib();
    
        add_mibdir("c:\\MyMIBFileDir\\");
    
        tree_head = read_mib(str);
    
        if ( tree_head )
        {
            //Successfully parsed the MIB
        }
    
        // Full traversal of the subtree
        for (tp = tree_head; tp; tp = tp->next_peer)
            // Here you can do custom parsing
    
        fclose(fp);
    
        return 0;
    }
    

    【讨论】:

    • 此代码从我的 PC 中读取所有 MIB除了我在 str 中指定的文件中的 MIB 树。出了点问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多