2 * snmptranslate.c - report or translate info about oid from mibs
4 * Update: 1998-07-17 <jhy@gsu.edu>
5 * Added support for dumping alternate oid reports (-t and -T options).
6 * Added more detailed (useful?) usage info.
8 /************************************************************************
9 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
13 Permission to use, copy, modify, and distribute this software and its
14 documentation for any purpose and without fee is hereby granted,
15 provided that the above copyright notice appear in all copies and that
16 both that copyright notice and this permission notice appear in
17 supporting documentation, and that the name of CMU not be
18 used in advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
22 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
23 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
24 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
26 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
28 ******************************************************************/
31 #include <net-snmp/net-snmp-config.h>
44 #include <sys/types.h>
46 #include <sys/select.h>
49 #include <netinet/in.h>
53 #include <net-snmp/utilities.h>
59 #include <net-snmp/config_api.h>
60 #include <net-snmp/output_api.h>
61 #include <net-snmp/mib_api.h>
63 int show_all_matched_objects(FILE *, const char *, oid *,
69 fprintf(stderr, "USAGE: snmptranslate [OPTIONS] OID [OID]...\n\n");
70 fprintf(stderr, " Version: %s\n", netsnmp_get_version());
71 fprintf(stderr, " Web: http://www.net-snmp.org/\n");
73 " Email: net-snmp-coders@lists.sourceforge.net\n\nOPTIONS:\n");
75 fprintf(stderr, " -h\t\t\tdisplay this help message\n");
76 fprintf(stderr, " -V\t\t\tdisplay package version number\n");
78 " -m MIB[:...]\t\tload given list of MIBs (ALL loads everything)\n");
80 " -M DIR[:...]\t\tlook in given list of directories for MIBs\n");
82 " -D TOKEN[,...]\tturn on debugging output for the specified TOKENs\n\t\t\t (ALL gives extremely verbose debugging output)\n");
83 fprintf(stderr, " -w WIDTH\t\tset width of tree and detail output\n");
85 " -T TRANSOPTS\t\tSet various options controlling report produced:\n");
87 "\t\t\t B: print all matching objects for a regex search\n");
88 fprintf(stderr, "\t\t\t d: print full details of the given OID\n");
89 fprintf(stderr, "\t\t\t p: print tree format symbol table\n");
90 fprintf(stderr, "\t\t\t a: print ASCII format symbol table\n");
91 fprintf(stderr, "\t\t\t l: enable labeled OID report\n");
92 fprintf(stderr, "\t\t\t o: enable OID report\n");
93 fprintf(stderr, "\t\t\t s: enable dotted symbolic report\n");
95 "\t\t\t t: enable alternate format symbolic suffix report\n");
97 " -P MIBOPTS\t\tToggle various defaults controlling mib parsing:\n");
98 snmp_mib_toggle_options_usage("\t\t\t ", stderr);
100 " -O OUTOPTS\t\tToggle various defaults controlling output display:\n");
101 snmp_out_toggle_options_usage("\t\t\t ", stderr);
103 " -I INOPTS\t\tToggle various defaults controlling input parsing:\n");
104 snmp_in_toggle_options_usage("\t\t\t ", stderr);
109 main(int argc, char *argv[])
112 char *current_name = NULL, *cp = NULL;
113 oid name[MAX_OID_LEN];
121 * usage: snmptranslate name
123 while ((arg = getopt(argc, argv, "Vhm:M:w:D:P:T:O:I:")) != EOF) {
130 setenv("MIBS", optarg, 1);
133 setenv("MIBDIRS", optarg, 1);
136 debug_register_tokens(optarg);
137 snmp_set_do_debugging(1);
140 fprintf(stderr, "NET-SNMP version: %s\n",
141 netsnmp_get_version());
145 width = atoi(optarg);
147 fprintf(stderr, "Invalid width specification: %s\n", optarg);
152 cp = snmp_mib_toggle_options(optarg);
154 fprintf(stderr, "Unknown parser option to -P: %c.\n", *cp);
160 cp = snmp_out_toggle_options(optarg);
162 fprintf(stderr, "Unknown OID option to -O: %c.\n", *cp);
168 cp = snmp_in_toggle_options(optarg);
170 fprintf(stderr, "Unknown OID option to -I: %c.\n", *cp);
176 for (cp = optarg; *cp; cp++) {
180 print_oid_report_enable_labeledoid();
184 print_oid_report_enable_oid();
188 print_oid_report_enable_symbolic();
192 print_oid_report_enable_suffix();
196 snmp_set_save_descriptions(1);
208 fprintf(stderr, "Invalid -T<lostpad> character: %c\n",
217 fprintf(stderr, "invalid option: -%c\n", arg);
224 init_snmp("snmpapp");
226 current_name = argv[optind];
228 if (current_name == NULL) {
234 print_mib_tree(stdout, get_tree_head(), width);
237 print_ascii_dump(stdout);
240 print_oid_report(stdout);
247 name_length = MAX_OID_LEN;
248 if (snmp_get_random_access()) {
249 if (!get_node(current_name, name, &name_length)) {
250 fprintf(stderr, "Unknown object identifier: %s\n",
254 } else if (find_all) {
255 if (0 == show_all_matched_objects(stdout, current_name,
257 description, width)) {
259 "Unable to find a matching object identifier for \"%s\"\n",
264 } else if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
265 NETSNMP_DS_LIB_REGEX_ACCESS)) {
266 if (0 == get_wild_node(current_name, name, &name_length)) {
268 "Unable to find a matching object identifier for \"%s\"\n",
273 if (!read_objid(current_name, name, &name_length)) {
274 snmp_perror(current_name);
282 tp = get_tree(name, name_length, get_tree_head());
285 "Unable to find a matching object identifier for \"%s\"\n",
289 print_mib_tree(stdout, tp, width);
291 print_objid(name, name_length);
293 print_description(name, name_length, width);
296 current_name = argv[++optind];
297 if (current_name != NULL)
299 } while (optind < argc);
305 * Show all object identifiers that match the pattern.
309 show_all_matched_objects(FILE * fp, const char *patmatch, oid * name, size_t * name_length, int f_desc, /* non-zero if descriptions should be shown */
312 int result, count = 0;
313 size_t savlen = *name_length;
314 clear_tree_flags(get_tree_head());
317 *name_length = savlen;
318 result = get_wild_node(patmatch, name, name_length);
323 fprint_objid(fp, name, *name_length);
325 fprint_description(fp, name, *name_length, width);