📄 snmp_parse_args.c
字号:
/* * snmp_parse_args.c */#include <net-snmp/net-snmp-config.h>#if HAVE_STDLIB_H#include <stdlib.h>#endif#if HAVE_UNISTD_H#include <unistd.h>#endif#if HAVE_STRING_H#include <string.h>#else#include <strings.h>#endif#include <sys/types.h>#include <stdio.h>#if HAVE_UNISTD_H#include <unistd.h>#endif#include <ctype.h>#if HAVE_NETINET_IN_H#include <netinet/in.h>#endif#if TIME_WITH_SYS_TIME# ifdef WIN32# include <sys/timeb.h># else# include <sys/time.h># endif# include <time.h>#else# if HAVE_SYS_TIME_H# include <sys/time.h># else# include <time.h># endif#endif#if HAVE_SYS_SELECT_H#include <sys/select.h>#endif#if HAVE_WINSOCK_H#include <winsock.h>#endif#if HAVE_NETDB_H#include <netdb.h>#endif#if HAVE_ARPA_INET_H#include <arpa/inet.h>#endif#include <net-snmp/types.h>#include <net-snmp/output_api.h>#include <net-snmp/config_api.h>#include <net-snmp/library/snmp_parse_args.h> /* for "internal" definitions */#include <net-snmp/utilities.h>#include <net-snmp/library/snmp_api.h>#include <net-snmp/library/snmp_client.h>#include <net-snmp/library/mib.h>#include <net-snmp/library/scapi.h>#include <net-snmp/library/keytools.h>#include <net-snmp/version.h>#include <net-snmp/library/parse.h>#include <net-snmp/library/snmpv3.h>#include <net-snmp/library/transform_oids.h>int random_access = 0;voidsnmp_parse_args_usage(FILE * outf){ fprintf(outf, "[OPTIONS] AGENT");}voidsnmp_parse_args_descriptions(FILE * outf){ fprintf(outf, " Version: %s\n", netsnmp_get_version()); fprintf(outf, " Web: http://www.net-snmp.org/\n"); fprintf(outf, " Email: net-snmp-coders@lists.sourceforge.net\n\nOPTIONS:\n"); fprintf(outf, " -h, --help\t\tdisplay this help message\n"); fprintf(outf, " -H\t\t\tdisplay configuration file directives understood\n"); fprintf(outf, " -v 1|2c|3\t\tspecifies SNMP version to use\n"); fprintf(outf, " -V, --version\t\tdisplay package version number\n"); fprintf(outf, "SNMP Version 1 or 2c specific\n"); fprintf(outf, " -c COMMUNITY\t\tset the community string\n"); fprintf(outf, "SNMP Version 3 specific\n"); fprintf(outf, " -a PROTOCOL\t\tset authentication protocol (MD5|SHA)\n"); fprintf(outf, " -A PASSPHRASE\t\tset authentication protocol pass phrase\n"); fprintf(outf, " -e ENGINE-ID\t\tset security engine ID (e.g. 800000020109840301)\n"); fprintf(outf, " -E ENGINE-ID\t\tset context engine ID (e.g. 800000020109840301)\n"); fprintf(outf, " -l LEVEL\t\tset security level (noAuthNoPriv|authNoPriv|authPriv)\n"); fprintf(outf, " -n CONTEXT\t\tset context name (e.g. bridge1)\n"); fprintf(outf, " -u USER-NAME\t\tset security name (e.g. bert)\n");#ifdef HAVE_AES fprintf(outf, " -x PROTOCOL\t\tset privacy protocol (DES|AES)\n");#else fprintf(outf, " -x PROTOCOL\t\tset privacy protocol (DES)\n");#endif fprintf(outf, " -X PASSPHRASE\t\tset privacy protocol pass phrase\n"); fprintf(outf, " -Z BOOTS,TIME\t\tset destination engine boots/time\n"); fprintf(outf, "General communication options\n"); fprintf(outf, " -r RETRIES\t\tset the number of retries\n"); fprintf(outf, " -t TIMEOUT\t\tset the request timeout (in seconds)\n"); fprintf(outf, "Debugging\n"); fprintf(outf, " -d\t\t\tdump input/output packets in hexadecimal\n"); fprintf(outf, " -D TOKEN[,...]\tturn on debugging output for the specified TOKENs\n\t\t\t (ALL gives extremely verbose debugging output)\n"); fprintf(outf, "General options\n"); fprintf(outf, " -m MIB[:...]\t\tload given list of MIBs (ALL loads everything)\n"); fprintf(outf, " -M DIR[:...]\t\tlook in given list of directories for MIBs\n"); fprintf(outf, " -P MIBOPTS\t\tToggle various defaults controlling MIB parsing:\n"); snmp_mib_toggle_options_usage("\t\t\t ", outf); fprintf(outf, " -O OUTOPTS\t\tToggle various defaults controlling output display:\n"); snmp_out_toggle_options_usage("\t\t\t ", outf); fprintf(outf, " -I INOPTS\t\tToggle various defaults controlling input parsing:\n"); snmp_in_toggle_options_usage("\t\t\t ", outf); fflush(outf);}#define BUF_SIZE 512voidhandle_long_opt(const char *myoptarg){ char *cp, *cp2; /* * else it's a long option, so process it like name=value */ cp = malloc(strlen(myoptarg) + 3); strcpy(cp, myoptarg); cp2 = strchr(cp, '='); if (!cp2 && !strchr(cp, ' ')) { /* * well, they didn't specify an argument as far as we * can tell. Give them a '1' as the argument (which * works for boolean tokens and a few others) and let * them suffer from there if it's not what they * wanted */ strcat(cp, " 1"); } else { /* * replace the '=' with a ' ' */ if (cp2) *cp2 = ' '; } netsnmp_config(cp); free(cp);}extern int snmpv3_options(char *optarg, netsnmp_session * session, char **Apsz, char **Xpsz, int argc, char *const *argv);intsnmp_parse_args(int argc, char *const *argv, netsnmp_session * session, const char *localOpts, void (*proc) (int, char *const *, int)){ int arg; char *cp; char *Apsz = NULL; char *Xpsz = NULL; char *Cpsz = NULL; char Opts[BUF_SIZE]; /* * initialize session to default values */ snmp_sess_init(session); strcpy(Opts, "Y:VhHm:M:O:I:P:D:dv:r:t:c:Z:e:E:n:u:l:x:X:a:A:p:T:-:3:"); if (localOpts) strcat(Opts, localOpts); /* * get the options */ DEBUGMSGTL(("snmp_parse_args", "starting: %d/%d\n", optind, argc)); for (arg = 0; arg < argc; arg++) { DEBUGMSGTL(("snmp_parse_args", " arg %d = %s\n", arg, argv[arg])); } optind = 1; while ((arg = getopt(argc, argv, Opts)) != EOF) { DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %c\n", optind, arg)); switch (arg) { case '-': if (strcasecmp(optarg, "help") == 0) { return (-1); } if (strcasecmp(optarg, "version") == 0) { fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version()); return (-2); } handle_long_opt(optarg); break; case 'V': fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version()); return (-2); case 'h': return (-1); break; case 'H': init_snmp("snmpapp"); fprintf(stderr, "Configuration directives understood:\n"); read_config_print_usage(" "); return (-2); case 'Y': netsnmp_config_remember(optarg); break; case 'm': setenv("MIBS", optarg, 1); break; case 'M': netsnmp_set_mib_directory(optarg); break; case 'O': cp = snmp_out_toggle_options(optarg); if (cp != NULL) { fprintf(stderr, "Unknown output option passed to -O: %c.\n", *cp); return (-1); } break; case 'I': cp = snmp_in_toggle_options(optarg); if (cp != NULL) { fprintf(stderr, "Unknown input option passed to -I: %c.\n", *cp); return (-1); } break; case 'P': cp = snmp_mib_toggle_options(optarg); if (cp != NULL) { fprintf(stderr, "Unknown parsing option passed to -P: %c.\n", *cp); return (-1); } break; case 'D': debug_register_tokens(optarg); snmp_set_do_debugging(1); break; case 'd': netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DUMP_PACKET, 1); break; case 'v': if (!strcmp(optarg, "1")) { session->version = SNMP_VERSION_1; } else if (!strcasecmp(optarg, "2c")) { session->version = SNMP_VERSION_2c; } else if (!strcasecmp(optarg, "3")) { session->version = SNMP_VERSION_3; } else { fprintf(stderr, "Invalid version specified after -v flag: %s\n", optarg); return (-1); } break; case 'p': fprintf(stderr, "Warning: -p option is no longer used - "); fprintf(stderr, "specify the remote host as HOST:PORT\n"); return (-1); break; case 'T': fprintf(stderr, "Warning: -T option is no longer used - "); fprintf(stderr, "specify the remote host as TRANSPORT:HOST\n"); return (-1); break; case 't': session->timeout = atoi(optarg) * 1000000L; if (session->timeout < 0 || !isdigit(optarg[0])) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -