2 * snmpbulkget.c - send SNMPv2 Bulk 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 <net-snmp/utilities.h>
40 #include <sys/types.h>
42 #include <netinet/in.h>
44 #if TIME_WITH_SYS_TIME
46 # include <sys/timeb.h>
48 # include <sys/time.h>
53 # include <sys/time.h>
59 #include <sys/select.h>
70 #include <arpa/inet.h>
73 #include <net-snmp/net-snmp-includes.h>
75 oid objid_mib[] = { 1, 3, 6, 1, 2, 1 };
76 int max_repetitions = 10;
77 int non_repeaters = 0;
79 oid name[MAX_OID_LEN];
87 fprintf(stderr, "USAGE: snmpbulkget ");
88 snmp_parse_args_usage(stderr);
89 fprintf(stderr, " OID [OID]...\n\n");
90 snmp_parse_args_descriptions(stderr);
92 " -C APPOPTS\t\tSet various application specific behaviours:\n");
93 fprintf(stderr, "\t\t\t n<NUM>: set non-repeaters to <NUM>\n");
94 fprintf(stderr, "\t\t\t r<NUM>: set max-repeaters to <NUM>\n");
99 optProc(int argc, char *const *argv, int opt)
109 if (*(optarg - 1) == 'r') {
110 max_repetitions = strtol(optarg, &endptr, 0);
112 non_repeaters = strtol(optarg, &endptr, 0);
115 if (endptr == optarg) {
117 * No number given -- error.
123 if (isspace(*optarg)) {
130 fprintf(stderr, "Unknown flag passed to -C: %c\n",
139 main(int argc, char *argv[])
141 netsnmp_session session, *ss;
143 netsnmp_pdu *response;
144 netsnmp_variable_list *vars;
152 * get the common command line arguments
154 switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
165 if (names < non_repeaters) {
166 fprintf(stderr, "snmpbulkget: need more objects than <nonrep>\n");
170 namep = name = (struct nameStruct *) calloc(names, sizeof(*name));
172 namep->name_len = MAX_OID_LEN;
173 if (snmp_parse_oid(argv[arg], namep->name, &namep->name_len) ==
175 snmp_perror(argv[arg]);
185 * open an SNMP session
187 ss = snmp_open(&session);
190 * diagnose snmp_open errors with the input netsnmp_session pointer
192 snmp_sess_perror("snmpbulkget", &session);
198 * create PDU for GETBULK request and add object name to request
200 pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
201 pdu->non_repeaters = non_repeaters;
202 pdu->max_repetitions = max_repetitions; /* fill the packet */
203 for (arg = 0; arg < names; arg++)
204 snmp_add_null_var(pdu, name[arg].name, name[arg].name_len);
209 status = snmp_synch_response(ss, pdu, &response);
210 if (status == STAT_SUCCESS) {
211 if (response->errstat == SNMP_ERR_NOERROR) {
213 * check resulting variables
215 for (vars = response->variables; vars;
216 vars = vars->next_variable)
217 print_variable(vars->name, vars->name_length, vars);
220 * error in response, print it
223 if (response->errstat == SNMP_ERR_NOSUCHNAME) {
224 printf("End of MIB.\n");
226 fprintf(stderr, "Error in packet.\nReason: %s\n",
227 snmp_errstring(response->errstat));
228 if (response->errindex != 0) {
229 fprintf(stderr, "Failed object: ");
230 for (count = 1, vars = response->variables;
231 vars && (count != response->errindex);
232 vars = vars->next_variable, count++)
235 fprint_objid(stderr, vars->name,
237 fprintf(stderr, "\n");
242 } else if (status == STAT_TIMEOUT) {
243 fprintf(stderr, "Timeout: No Response from %s\n",
247 } else { /* status == STAT_ERROR */
248 snmp_sess_perror("snmpbulkget", ss);
254 snmp_free_pdu(response);