Sometimes I want to know the OID from a name and vice versa.
I'm done doing these by hand. Diff below does this for me:

$ ./obj/snmp mibtree -On snmpTrapOID.0
.1.3.6.1.6.3.1.1.4.1.0

OK?

martijn@

Index: snmp.1
===================================================================
RCS file: /cvs/src/usr.bin/snmp/snmp.1,v
retrieving revision 1.14
diff -u -p -r1.14 snmp.1
--- snmp.1      8 Aug 2020 07:11:47 -0000       1.14
+++ snmp.1      14 Sep 2020 14:50:09 -0000
@@ -49,6 +49,7 @@
 .Nm
 .Cm mibtree
 .Op Fl O Ar fns
+.Op Ar oid ...
 .Sh DESCRIPTION
 The
 .Nm
@@ -165,8 +166,12 @@ An SNMP based version of the
 .Xr df 1
 command.
 If no size suffix is shown the sizes are in kilobytes.
-.It Nm Cm mibtree Op Fl O Ar fnS
+.It Nm Cm mibtree Oo Fl O Ar fnS Oc Op Ar oid ...
 Dump the tree of compiled-in MIB objects.
+If
+.Ar oid
+is specified it wil print the objects in the requested output format if
+available, or print a warning if the object can't be found.
 .El
 .Pp
 The
Index: snmpc.c
===================================================================
RCS file: /cvs/src/usr.bin/snmp/snmpc.c,v
retrieving revision 1.29
diff -u -p -r1.29 snmpc.c
--- snmpc.c     12 Sep 2020 18:11:43 -0000      1.29
+++ snmpc.c     14 Sep 2020 14:50:10 -0000
@@ -80,7 +80,7 @@ struct snmp_app snmp_apps[] = {
        { "set", 1, NULL, "agent oid type value [oid type value] ...", 
snmpc_set },
        { "trap", 1, NULL, "agent uptime oid [oid type value] ...", snmpc_trap 
},
        { "df", 1, "C:", "[-Ch] [-Cr<maxrep>] agent", snmpc_df },
-       { "mibtree", 0, "O:", "[-O fnS]", snmpc_mibtree }
+       { "mibtree", 0, "O:", "[-O fnS] [oid] ...", snmpc_mibtree }
 };
 struct snmp_app *snmp_app = NULL;
 
@@ -1060,11 +1060,25 @@ int
 snmpc_mibtree(int argc, char *argv[])
 {
        struct oid *oid;
+       struct ber_oid soid;
        char buf[BUFSIZ];
+       int i;
 
-       for (oid = NULL; (oid = smi_foreach(oid)) != NULL;) {
-               smi_oid2string(&oid->o_id, buf, sizeof(buf), oid_lookup);
-               printf("%s\n", buf);
+       if (argc == 0) {
+               for (oid = NULL; (oid = smi_foreach(oid)) != NULL;) {
+                       smi_oid2string(&oid->o_id, buf, sizeof(buf),
+                           oid_lookup);
+                       printf("%s\n", buf);
+               }
+       } else {
+               for (i = 0; i < argc; i++) {
+                       if (smi_string2oid(argv[i], &soid) == -1) {
+                               warnx("%s: Unknown object identifier", argv[i]);
+                               continue;
+                       }
+                       smi_oid2string(&soid, buf, sizeof(buf), oid_lookup);
+                       printf("%s\n", buf);
+               }
        }
        return 0;
 }

Reply via email to