The following C program uses libsmi to print the names and numerical OIDs of all nodes below a given node of the OID tree (download example.c).
/*
* smisubtree - print the names of all nodes below a given OID.
*
* Build: cc -o smisubtree example.c $(pkg-config --cflags --libs libsmi)
* Usage: ./smisubtree ifTable
* ./smisubtree 1.3.6.1.2.1.2
*/
#include <stdio.h>
#include <stdlib.h>
#include <smi.h>
int main(int argc, char *argv[])
{
SmiNode *smiNode;
unsigned int oidlen;
if (argc != 2) {
fprintf(stderr, "Usage: smisubtree oid\n");
return EXIT_FAILURE;
}
smiInit("smisubtree"); /* reads smi.conf, [smisubtree] section */
smiLoadModule("IF-MIB"); /* a few modules to search in */
smiLoadModule("IP-MIB");
smiNode = smiGetNode(NULL, argv[1]);
if (!smiNode) {
fprintf(stderr, "smisubtree: unknown node `%s'\n", argv[1]);
smiExit();
return EXIT_FAILURE;
}
oidlen = smiNode->oidlen;
do {
char *oid = smiRenderOID(smiNode->oidlen, smiNode->oid, SMI_RENDER_NUMERIC);
int indent = (int)(smiNode->oidlen - oidlen) * 2;
printf("%*s%-*s %s\n", indent, "", 32 - indent, smiNode->name, oid);
smiFree(oid);
smiNode = smiGetNextNode(smiNode, SMI_NODEKIND_ANY);
} while (smiNode && smiNode->oidlen > oidlen);
smiExit();
return EXIT_SUCCESS;
} Compile and run it like this:
$ cc -o smisubtree example.c $(pkg-config --cflags --libs libsmi)
$ ./smisubtree ifTable
ifTable 1.3.6.1.2.1.2.2
ifEntry 1.3.6.1.2.1.2.2.1
ifIndex 1.3.6.1.2.1.2.2.1.1
ifDescr 1.3.6.1.2.1.2.2.1.2
ifType 1.3.6.1.2.1.2.2.1.3
... smiGetNode() accepts names (ifTable, IF-MIB::ifTable) as well as numerical OIDs (1.3.6.1.2.1.2.2). The nodes of a subtree are visited in OID order with smiGetNextNode(); the walk ends at the first node that is not below the start node. The tools in the libsmi distribution, e.g. smiquery.c, are further examples. See the library and the manual pages for the API.