2 * snmpset.c - send snmp SET 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/net-snmp-includes.h>
79 fprintf(stderr, "USAGE: snmpset ");
80 snmp_parse_args_usage(stderr);
81 fprintf(stderr, " OID TYPE VALUE [OID TYPE VALUE]...\n\n");
82 snmp_parse_args_descriptions(stderr);
84 " -C APPOPTS\t\tSet various application specific behaviours:\n");
85 fprintf(stderr, "\t\t\t q: don't print results on success\n");
86 fprintf(stderr, "\n TYPE: one of i, u, t, a, o, s, x, d, b, n\n");
88 "\ti: INTEGER, u: unsigned INTEGER, t: TIMETICKS, a: IPADDRESS\n");
90 "\to: OBJID, s: STRING, x: HEX STRING, d: DECIMAL STRING, b: BITS\n");
91 #ifdef OPAQUE_SPECIAL_TYPES
93 "\tU: unsigned int64, I: signed int64, F: float, D: double\n");
94 #endif /* OPAQUE_SPECIAL_TYPES */
102 optProc(int argc, char *const *argv, int opt)
113 fprintf(stderr, "Unknown flag passed to -C: %c\n",
122 main(int argc, char *argv[])
124 netsnmp_session session, *ss;
125 netsnmp_pdu *pdu, *response = NULL;
126 netsnmp_variable_list *vars;
129 int current_name = 0;
130 int current_type = 0;
131 int current_value = 0;
135 oid name[MAX_OID_LEN];
140 putenv(strdup("POSIXLY_CORRECT=1"));
143 * get the common command line arguments
145 switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
156 fprintf(stderr, "Missing object name\n");
162 * get object names, types, and values
164 for (; arg < argc; arg++) {
165 DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %s %s %s\n",
166 arg,argv[arg], arg+1 < argc ? argv[arg+1] : NULL,
167 arg+2 < argc ? argv[arg+2] : NULL));
168 names[current_name++] = argv[arg++];
170 switch (*argv[arg]) {
181 #ifdef OPAQUE_SPECIAL_TYPES
186 #endif /* OPAQUE_SPECIAL_TYPES */
187 types[current_type++] = *argv[arg++];
190 fprintf(stderr, "%s: Bad object type: %c\n", argv[arg - 1],
195 fprintf(stderr, "%s: Needs type and value\n", argv[arg - 1]);
199 values[current_value++] = argv[arg];
201 fprintf(stderr, "%s: Needs value\n", argv[arg - 2]);
209 * open an SNMP session
211 ss = snmp_open(&session);
214 * diagnose snmp_open errors with the input netsnmp_session pointer
216 snmp_sess_perror("snmpset", &session);
222 * create PDU for SET request and add object names and values to request
224 pdu = snmp_pdu_create(SNMP_MSG_SET);
225 for (count = 0; count < current_name; count++) {
226 name_length = MAX_OID_LEN;
227 if (snmp_parse_oid(names[count], name, &name_length) == NULL) {
228 snmp_perror(names[count]);
232 (pdu, name, name_length, types[count], values[count])) {
233 snmp_perror(names[count]);
246 status = snmp_synch_response(ss, pdu, &response);
247 if (status == STAT_SUCCESS) {
248 if (response->errstat == SNMP_ERR_NOERROR) {
250 for (vars = response->variables; vars;
251 vars = vars->next_variable)
252 print_variable(vars->name, vars->name_length, vars);
255 fprintf(stderr, "Error in packet.\nReason: %s\n",
256 snmp_errstring(response->errstat));
257 if (response->errindex != 0) {
258 fprintf(stderr, "Failed object: ");
259 for (count = 1, vars = response->variables;
260 vars && (count != response->errindex);
261 vars = vars->next_variable, count++);
263 fprint_objid(stderr, vars->name, vars->name_length);
264 fprintf(stderr, "\n");
268 } else if (status == STAT_TIMEOUT) {
269 fprintf(stderr, "Timeout: No Response from %s\n",
272 } else { /* status == STAT_ERROR */
273 snmp_sess_perror("snmpset", ss);
278 snmp_free_pdu(response);