2011-09-07 58 views

回答

10

下面是使用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; 
}