~ubuntu-branches/ubuntu/trusty/net-snmp/trusty

« back to all changes in this revision

Viewing changes to apps/snmpget.c

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2004-09-13 12:06:21 UTC
  • Revision ID: james.westby@ubuntu.com-20040913120621-g952ntonlleihcvm
Tags: upstream-5.1.1
ImportĀ upstreamĀ versionĀ 5.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * snmpget.c - send snmp GET requests to a network entity.
 
3
 *
 
4
 */
 
5
/***********************************************************************
 
6
        Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
 
7
 
 
8
                      All Rights Reserved
 
9
 
 
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.  
 
17
 
 
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
 
24
SOFTWARE.
 
25
******************************************************************/
 
26
#include <net-snmp/net-snmp-config.h>
 
27
 
 
28
#if HAVE_STDLIB_H
 
29
#include <stdlib.h>
 
30
#endif
 
31
#if HAVE_UNISTD_H
 
32
#include <unistd.h>
 
33
#endif
 
34
#if HAVE_STRING_H
 
35
#include <string.h>
 
36
#else
 
37
#include <strings.h>
 
38
#endif
 
39
#include <sys/types.h>
 
40
#if HAVE_NETINET_IN_H
 
41
#include <netinet/in.h>
 
42
#endif
 
43
#include <stdio.h>
 
44
#include <ctype.h>
 
45
#if TIME_WITH_SYS_TIME
 
46
# ifdef WIN32
 
47
#  include <sys/timeb.h>
 
48
# else
 
49
#  include <sys/time.h>
 
50
# endif
 
51
# include <time.h>
 
52
#else
 
53
# if HAVE_SYS_TIME_H
 
54
#  include <sys/time.h>
 
55
# else
 
56
#  include <time.h>
 
57
# endif
 
58
#endif
 
59
#if HAVE_SYS_SELECT_H
 
60
#include <sys/select.h>
 
61
#endif
 
62
#if HAVE_WINSOCK_H
 
63
#include <winsock.h>
 
64
#endif
 
65
#if HAVE_NETDB_H
 
66
#include <netdb.h>
 
67
#endif
 
68
#if HAVE_ARPA_INET_H
 
69
#include <arpa/inet.h>
 
70
#endif
 
71
 
 
72
#include <net-snmp/utilities.h>
 
73
 
 
74
#include <net-snmp/net-snmp-includes.h>
 
75
 
 
76
int             failures = 0;
 
77
 
 
78
#define NETSNMP_DS_APP_DONT_FIX_PDUS 0
 
79
 
 
80
static void
 
81
optProc(int argc, char *const *argv, int opt)
 
82
{
 
83
    switch (opt) {
 
84
    case 'C':
 
85
        while (*optarg) {
 
86
            switch (*optarg++) {
 
87
            case 'f':
 
88
                netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID, 
 
89
                                          NETSNMP_DS_APP_DONT_FIX_PDUS);
 
90
                break;
 
91
            default:
 
92
                fprintf(stderr, "Unknown flag passed to -C: %c\n",
 
93
                        optarg[-1]);
 
94
                exit(1);
 
95
            }
 
96
        }
 
97
        break;
 
98
    }
 
99
}
 
100
 
 
101
void
 
102
usage(void)
 
103
{
 
104
    fprintf(stderr, "USAGE: snmpget ");
 
105
    snmp_parse_args_usage(stderr);
 
106
    fprintf(stderr, " OID [OID]...\n\n");
 
107
    snmp_parse_args_descriptions(stderr);
 
108
    fprintf(stderr,
 
109
            "  -C APPOPTS\t\tSet various application specific behaviours:\n");
 
110
    fprintf(stderr,
 
111
            "\t\t\t  f:  do not fix errors and retry the request\n");
 
112
}
 
113
 
 
114
int
 
115
main(int argc, char *argv[])
 
116
{
 
117
    netsnmp_session session, *ss;
 
118
    netsnmp_pdu    *pdu;
 
119
    netsnmp_pdu    *response;
 
120
    netsnmp_variable_list *vars;
 
121
    int             arg;
 
122
    int             count;
 
123
    int             current_name = 0;
 
124
    char           *names[SNMP_MAX_CMDLINE_OIDS];
 
125
    oid             name[MAX_OID_LEN];
 
126
    size_t          name_length;
 
127
    int             status;
 
128
    int             exitval = 0;
 
129
 
 
130
    /*
 
131
     * get the common command line arguments 
 
132
     */
 
133
    switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
 
134
    case -2:
 
135
        exit(0);
 
136
    case -1:
 
137
        usage();
 
138
        exit(1);
 
139
    default:
 
140
        break;
 
141
    }
 
142
 
 
143
    if (arg >= argc) {
 
144
        fprintf(stderr, "Missing object name\n");
 
145
        usage();
 
146
        exit(1);
 
147
    }
 
148
    if ((argc - arg) > SNMP_MAX_CMDLINE_OIDS) {
 
149
        fprintf(stderr, "Too many object identifiers specified. ");
 
150
        fprintf(stderr, "Only %d allowed in one request.\n", SNMP_MAX_CMDLINE_OIDS);
 
151
        usage();
 
152
        exit(1);
 
153
    }
 
154
 
 
155
    /*
 
156
     * get the object names 
 
157
     */
 
158
    for (; arg < argc; arg++)
 
159
        names[current_name++] = argv[arg];
 
160
 
 
161
    SOCK_STARTUP;
 
162
 
 
163
 
 
164
    /*
 
165
     * Open an SNMP session.
 
166
     */
 
167
    ss = snmp_open(&session);
 
168
    if (ss == NULL) {
 
169
        /*
 
170
         * diagnose snmp_open errors with the input netsnmp_session pointer 
 
171
         */
 
172
        snmp_sess_perror("snmpget", &session);
 
173
        SOCK_CLEANUP;
 
174
        exit(1);
 
175
    }
 
176
 
 
177
 
 
178
    /*
 
179
     * Create PDU for GET request and add object names to request.
 
180
     */
 
181
    pdu = snmp_pdu_create(SNMP_MSG_GET);
 
182
    for (count = 0; count < current_name; count++) {
 
183
        name_length = MAX_OID_LEN;
 
184
        if (!snmp_parse_oid(names[count], name, &name_length)) {
 
185
            snmp_perror(names[count]);
 
186
            failures++;
 
187
        } else
 
188
            snmp_add_null_var(pdu, name, name_length);
 
189
    }
 
190
    if (failures) {
 
191
        SOCK_CLEANUP;
 
192
        exit(1);
 
193
    }
 
194
 
 
195
 
 
196
    /*
 
197
     * Perform the request.
 
198
     *
 
199
     * If the Get Request fails, note the OID that caused the error,
 
200
     * "fix" the PDU (removing the error-prone OID) and retry.
 
201
     */
 
202
  retry:
 
203
    status = snmp_synch_response(ss, pdu, &response);
 
204
    if (status == STAT_SUCCESS) {
 
205
        if (response->errstat == SNMP_ERR_NOERROR) {
 
206
            for (vars = response->variables; vars;
 
207
                 vars = vars->next_variable)
 
208
                print_variable(vars->name, vars->name_length, vars);
 
209
 
 
210
        } else {
 
211
            fprintf(stderr, "Error in packet\nReason: %s\n",
 
212
                    snmp_errstring(response->errstat));
 
213
 
 
214
            if (response->errindex != 0) {
 
215
                fprintf(stderr, "Failed object: ");
 
216
                for (count = 1, vars = response->variables;
 
217
                     vars && count != response->errindex;
 
218
                     vars = vars->next_variable, count++)
 
219
                    /*EMPTY*/;
 
220
                if (vars) {
 
221
                    fprint_objid(stderr, vars->name, vars->name_length);
 
222
                }
 
223
                fprintf(stderr, "\n");
 
224
            }
 
225
            exitval = 2;
 
226
 
 
227
            /*
 
228
             * retry if the errored variable was successfully removed 
 
229
             */
 
230
            if (!netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
 
231
                                        NETSNMP_DS_APP_DONT_FIX_PDUS)) {
 
232
                pdu = snmp_fix_pdu(response, SNMP_MSG_GET);
 
233
                snmp_free_pdu(response);
 
234
                response = NULL;
 
235
                if (pdu != NULL) {
 
236
                    goto retry;
 
237
                }
 
238
            }
 
239
        }                       /* endif -- SNMP_ERR_NOERROR */
 
240
 
 
241
    } else if (status == STAT_TIMEOUT) {
 
242
        fprintf(stderr, "Timeout: No Response from %s.\n",
 
243
                session.peername);
 
244
        exitval = 1;
 
245
 
 
246
    } else {                    /* status == STAT_ERROR */
 
247
        snmp_sess_perror("snmpget", ss);
 
248
        exitval = 1;
 
249
 
 
250
    }                           /* endif -- STAT_SUCCESS */
 
251
 
 
252
 
 
253
    if (response)
 
254
        snmp_free_pdu(response);
 
255
    snmp_close(ss);
 
256
    SOCK_CLEANUP;
 
257
    return exitval;
 
258
 
 
259
}                               /* end main() */