2 * snmpget.c - send snmp GET requests to a network entity.
5 /***********************************************************************
6 Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
10 Permission to use, copy, modify, and distribute this software and its
11 documentation for any purpose and without fee is hereby granted,
12 provided that the above copyright notice appear in all copies and that
13 both that copyright notice and this permission notice appear in
14 supporting documentation, and that the name of CMU not be
15 used in advertising or publicity pertaining to distribution of the
16 software without specific, written prior permission.
18 CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
19 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
20 CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
21 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
23 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
25 ******************************************************************/
26 #include <net-snmp/net-snmp-config.h>
39 #include <sys/types.h>
41 #include <netinet/in.h>
45 #if TIME_WITH_SYS_TIME
47 # include <sys/timeb.h>
49 # include <sys/time.h>
54 # include <sys/time.h>
60 #include <sys/select.h>
69 #include <arpa/inet.h>
72 #include <net-snmp/utilities.h>
74 #include <net-snmp/net-snmp-includes.h>
78 #define NETSNMP_DS_APP_DONT_FIX_PDUS 0
81 optProc(int argc, char *const *argv, int opt)
88 netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
89 NETSNMP_DS_APP_DONT_FIX_PDUS);
92 fprintf(stderr, "Unknown flag passed to -C: %c\n",
104 fprintf(stderr, "USAGE: snmpget ");
105 snmp_parse_args_usage(stderr);
106 fprintf(stderr, " OID [OID]...\n\n");
107 snmp_parse_args_descriptions(stderr);
109 " -C APPOPTS\t\tSet various application specific behaviours:\n");
111 "\t\t\t f: do not fix errors and retry the request\n");
115 main(int argc, char *argv[])
117 netsnmp_session session, *ss;
119 netsnmp_pdu *response;
120 netsnmp_variable_list *vars;
123 int current_name = 0;
125 oid name[MAX_OID_LEN];
131 * get the common command line arguments
133 switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
144 fprintf(stderr, "Missing object name\n");
150 * get the object names
152 for (; arg < argc; arg++)
153 names[current_name++] = argv[arg];
159 * Open an SNMP session.
161 ss = snmp_open(&session);
164 * diagnose snmp_open errors with the input netsnmp_session pointer
166 snmp_sess_perror("snmpget", &session);
173 * Create PDU for GET request and add object names to request.
175 pdu = snmp_pdu_create(SNMP_MSG_GET);
176 for (count = 0; count < current_name; count++) {
177 name_length = MAX_OID_LEN;
178 if (!snmp_parse_oid(names[count], name, &name_length)) {
179 snmp_perror(names[count]);
182 snmp_add_null_var(pdu, name, name_length);
191 * Perform the request.
193 * If the Get Request fails, note the OID that caused the error,
194 * "fix" the PDU (removing the error-prone OID) and retry.
197 status = snmp_synch_response(ss, pdu, &response);
198 if (status == STAT_SUCCESS) {
199 if (response->errstat == SNMP_ERR_NOERROR) {
200 for (vars = response->variables; vars;
201 vars = vars->next_variable)
202 print_variable(vars->name, vars->name_length, vars);
205 fprintf(stderr, "Error in packet\nReason: %s\n",
206 snmp_errstring(response->errstat));
208 if (response->errindex != 0) {
209 fprintf(stderr, "Failed object: ");
210 for (count = 1, vars = response->variables;
211 vars && count != response->errindex;
212 vars = vars->next_variable, count++)
215 fprint_objid(stderr, vars->name, vars->name_length);
217 fprintf(stderr, "\n");
222 * retry if the errored variable was successfully removed
224 if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
225 NETSNMP_DS_APP_DONT_FIX_PDUS)) {
226 pdu = snmp_fix_pdu(response, SNMP_MSG_GET);
227 snmp_free_pdu(response);
233 } /* endif -- SNMP_ERR_NOERROR */
235 } else if (status == STAT_TIMEOUT) {
236 fprintf(stderr, "Timeout: No Response from %s.\n",
240 } else { /* status == STAT_ERROR */
241 snmp_sess_perror("snmpget", ss);
244 } /* endif -- STAT_SUCCESS */
248 snmp_free_pdu(response);