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;
132
char *names[SNMP_MAX_CMDLINE_OIDS];
133
char types[SNMP_MAX_CMDLINE_OIDS];
134
char *values[SNMP_MAX_CMDLINE_OIDS];
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");
160
if ((argc - arg) > 3*SNMP_MAX_CMDLINE_OIDS) {
161
fprintf(stderr, "Too many assignments specified. ");
162
fprintf(stderr, "Only %d allowed in one request.\n", SNMP_MAX_CMDLINE_OIDS);
168
* get object names, types, and values
170
for (; arg < argc; arg++) {
171
DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %s %s %s\n",
172
arg,argv[arg], arg+1 < argc ? argv[arg+1] : NULL,
173
arg+2 < argc ? argv[arg+2] : NULL));
174
names[current_name++] = argv[arg++];
176
switch (*argv[arg]) {
187
#ifdef OPAQUE_SPECIAL_TYPES
192
#endif /* OPAQUE_SPECIAL_TYPES */
193
types[current_type++] = *argv[arg++];
196
fprintf(stderr, "%s: Bad object type: %c\n", argv[arg - 1],
201
fprintf(stderr, "%s: Needs type and value\n", argv[arg - 1]);
205
values[current_value++] = argv[arg];
207
fprintf(stderr, "%s: Needs value\n", argv[arg - 2]);
215
* open an SNMP session
217
ss = snmp_open(&session);
220
* diagnose snmp_open errors with the input netsnmp_session pointer
222
snmp_sess_perror("snmpset", &session);
228
* create PDU for SET request and add object names and values to request
230
pdu = snmp_pdu_create(SNMP_MSG_SET);
231
for (count = 0; count < current_name; count++) {
232
name_length = MAX_OID_LEN;
233
if (snmp_parse_oid(names[count], name, &name_length) == NULL) {
234
snmp_perror(names[count]);
238
(pdu, name, name_length, types[count], values[count])) {
239
snmp_perror(names[count]);
252
status = snmp_synch_response(ss, pdu, &response);
253
if (status == STAT_SUCCESS) {
254
if (response->errstat == SNMP_ERR_NOERROR) {
256
for (vars = response->variables; vars;
257
vars = vars->next_variable)
258
print_variable(vars->name, vars->name_length, vars);
261
fprintf(stderr, "Error in packet.\nReason: %s\n",
262
snmp_errstring(response->errstat));
263
if (response->errindex != 0) {
264
fprintf(stderr, "Failed object: ");
265
for (count = 1, vars = response->variables;
266
vars && (count != response->errindex);
267
vars = vars->next_variable, count++);
269
fprint_objid(stderr, vars->name, vars->name_length);
270
fprintf(stderr, "\n");
274
} else if (status == STAT_TIMEOUT) {
275
fprintf(stderr, "Timeout: No Response from %s\n",
278
} else { /* status == STAT_ERROR */
279
snmp_sess_perror("snmpset", ss);
284
snmp_free_pdu(response);