Content-type: text/html Man page of mib2c


Section: System Administration Commands (1M)
Updated: 23 Jan 2004
Index Return to Main Contents


mib2c - produces template code from MIB definitions  


/usr/sfw/bin/mib2c [-h] -c configfile [-f outname] [-i] [-q] [-S var=val] mibnode [mibnode...]  


The mib2c tool is designed to take a portion of the MIB tree (as defined by a MIB file) and generate the template C code necessary to implement the corresponding MIB module.

To implement a new MIB module, three files are necessary:

• MIB definition file

• C header file

• C implementation file

The mib2c tool uses the MIB definition file to produce the two C code files. Thus, mib2c generates a template that you can edit to add logic necessary to obtain information from the operating system or application to complete the module.

The operand mibnode is the top level MIB node for which you want to generate code. You must give mib2c a MIB node (for example, ifTable), not a MIB file, on the command line. This distinction is a common source of user error.

The mib2c tool accepts both SMIv1 and SMIv2 MIBs.

mib2c needs to be able to find and load a MIB file in order to generate C code for the MIB. To enable mib2c to find the MIB file, set the MIBS environment variable to include the MIB file you are using. An example of setting this environment variable is:




The first example ensures that mib2c finds the NET-SNMP-TUTORIAL-MIB MIB, in addition to the default MIB modules. The default list of MIB modules is set when the suite is first configured and built. The list corresponds to the list of modules that the agent supports. The second example ensures that mib2c finds all MIBs in the search location for MIB files. The default search location for MIB files is DATADIR/snmp/mibs. This search location can be modified by the MIBDIRS environment variable.

Both the MIB files to be loaded and the MIB file search location can also be configured in the snmp.conf file. Please see snmp.conf(4) for more information.

The generated .c and .h files are created in the current working directory.  


The following options are supported:


Display a help message.

-c configfile

Use configfile when generating code. These files are searched for first in the current directory and then in the DATADIR directory, which is where the default mib2c configuration files are located. Running mib2c without the -c configfile option displays a description of the valid values for configfile, that is, the available configuration files, including new ones that you might have created.

For example:

% mib2c ifTable

...displays the contents of the mib2.conf file, which displays hints on choosing the best configfile option for the mibnode.

The following values are supported for configfile:


See EXAMPLES for commands you can use to generate code for scalar objects, tables, header files, and for SunOS 4.x code.

-f outname

Places the output code into outname.c and outname.h. In most cases, mib2c places the output code into files with names that correspond to the group names for which it is generating code.


Do not run indent in the resulting code. Omitting this option results in indent error messages. These can safely be ignored. For example:

% /usr/sfw/bin/mib2c -c mib2c.scalar.conf ifTable
writing to ifTable.h
writing to ifTable.c
running indent on ifTable.h
indent: Command line: unknown parameter "-orig"
running indent on ifTable.c
indent: Command line: unknown parameter "-orig"
% ls
ifTable.c   ifTable.h
% rm i*
% /usr/sfw/bin/mib2c -c mib2c.scalar.conf -i ifTable
writing to ifTable.h
writing to ifTable.c

In the first invocation of mib2c, above, the indent errors are of no consequence.


Run in "quiet" mode, which minimizes the status messages mib2c generates.

-S var=val

Preset a variable var in the mib2c.*.conf file to the value val. None of the existing mib2c configuration files (mib2c.*.conf) currently makes use of this feature. Consider this option available only for future use.



Example 1: Generating Code for Scalar Objects

If you are writing code for some scalars, run:

% mib2c -c mib2c.scalar.conf mibnode

If you want to magically "tie" integer variables to integer scalars, use:

% mib2c -c mib2c.int_watch.conf mibnode

Example 2: Generating Code for Tables

Consider the case where:

• You need to "iterate" over your table data to find the correct data for the SNMP row being accessed.

• Your table data is not kept within the agent (for example, it is in the kernel and not in the memory of the agent itself).

Under such conditions, use a command such as:

% mib2c -c mib2c.iterate.conf mibnode

You can find a similar example in agent/mibgroup/mibII/vacm_context.c.

If your table data is kept in the agent (that is, it is not located in an external source) and is purely data-driven (that is, you do not need to perform any work when a set occurs), you can use a command such as the following:

% mib2c -c mib2c.create-dataset.conf mibnode

See agent/mibgroup/examples/data_set.c for a similar example.

If your table data is kept in the agent (that is, it is not located in an external source) and you can keep your data sorted by the table index, but you do need to perform work when a set occurs, use a command such as the following:

% mib2c -c mib2c.array-user.conf mibnode

Example 3: Generating Header File Definitions

To generate just a header with a define for each column number in your table, enter a command such as:

% mib2c -c mib2c.column_defines.conf mibnode

To generate only a header with a define for each enum for any column containing enums, enter:

% mib2c -c mib2c.column_enums.conf mibnode

Example 4: Generating Code for the SunOS 4.X Line of Code

The following command generates code for SunOS 4.x:

% mib2c -c mib2c.old-api.conf mibnode

Example 5: Generating Code for ucdDemoPublic

The command below generates C template code for the header and implementation files to implement UCD-DEMO-MIB::ucdDemoPublic.

% mib2c -c mib2c.scalar.conf ucdDemoPublic

writing to ucdDemoPublic.h
writing to ucdDemoPublic.c
running indent on ucdDemoPublic.h
running indent on ucdDemoPublic.c

The resulting ucdDemoPublic.c and ucdDemoPublic.h files are generated in the current working directory.

Example 6: Generating Code for tcpConnTable

The command below generates C template code for the header and implementation files for the module to implement TCP-MIB::tcpConnTable.

% mib2c -c mib2c.iterate.conf tcpConnTable
writing to tcpConnTable.h
writing to tcpConnTable.c
running indent on tcpConnTable.h
running indent on tcpConnTable.c

The resulting tcpConnTable.c and tcpConnTable.h files are generated in the current working directory.  


0 Successful completion.

1 A usage syntax error. A usage message is displayed.



See attributes(5) for descriptions of the following attributes:

Interface StabilityExternal



snmpcmd(1M), snmp.conf(4), attributes(5)




This document was created by man2html, using the manual pages.
Time: 02:37:05 GMT, October 02, 2010