📄 snmptalk.c
字号:
{ "tos", do_v3_tos }, { "con_name", do_v3_con_name }, { "con_id", do_v3_con_id }, { "sec_name", do_v3_sec_name }, { "sec_id", do_v3_sec_id }, { "key_chg", do_v3_key_change }, { "own_chg", do_v3_own_key_change },#if (INSTALL_ENVOY_SNMP_V3_NOTIFY) && (INSTALL_ATTACHE) { "send_trap", do_sendtrap },#endif#endif#if INSTALL_ENVOY_SNMP_VERSION_USEC { "agent", do_agent }, { "qos", do_qos }, { "context", do_context },#endif { 0, 0 } /* the end */};static struct cmd *command_lookup(char *name){ struct cmd *cmd; for (cmd = commands; cmd->name; cmd++) if (STRNICMP(name, cmd->name, STRLEN(name)) == 0) return cmd; return 0;}static int command_run(struct sty *sty, int argc, char *argv[]){ struct cmd *cmd = command_lookup(argv[0]); if (!cmd || !cmd->rtn) return !0; if (alt_display) sty_puts(sty, "sTaRt\n"); return(cmd->rtn(sty, help_none, argc, argv));}static void command_exec(struct sty *sty, char *line, void *cookie){ char *argv[30]; int argc; if (!line) /* EOF? */ return; /* Yeah, ok, no cleanup, just done */ argc = parse_line(line, argv, sizeof(argv)/sizeof(*argv)); if (argc < 0 || argc > sizeof(argv)/sizeof(*argv)) { sty_puts(sty, "% couldn't parse command\n"); } else if (argc) { /* Don't complain about blank line */ null_cmd = 0; if (command_run(sty, argc, argv) == 0) return; } else null_cmd = 1; command_next(sty);}void command_next(struct sty *sty){ if (quit_flag) snark_exit(0); else { if (alt_display && (null_cmd == 0)) sty_puts(sty, "EnD\n"); sty_read(sty, command_exec, prompt, 0); }}/* * Set this variable to be the object ID of your product. You need to * allocate a number under ENTERPRISES by asking the IANA for it - see * the SNMP documentation for more information. * * This is only used when snmptalk is built to act as an agent in addition * to being a manager (currently only when Attache is installed), but it's * easiest just to initialize it here. */#define PRODUCT_ID_MAJOR_VERSION_INDEX 9#define PRODUCT_ID_MINOR_VERSION_INDEX 10static OIDC_T snmp_product_id[] = { /* ISO */ 1, /* ORG */ 3, /* DOD */ 6, /* INTERNET */ 1, /* PRIVATE */ 4, /* ENTERPRISES */ 1, /* Epilogue Technology */ 12, /* Products */ 7, /* SNMPTALK */ 2, /* Major Version */ 3, /* Minor Version */ 0xFFFF, };OBJ_ID_T var_sysObjectID = { sizeof(snmp_product_id)/sizeof(OIDC_T), snmp_product_id};/* * Initialization routines. */static struct { int argc; char **argv; int envc; char **envv;} setup_args; void banner(struct sty *sty){ sty_printf(sty, "SNMPTALK - Version %d.%d\n", snmptalk_major_version, snmptalk_minor_version); sty_puts(sty, " Copyright 1991 - 2002 Wind River Systems, Inc."); sty_puts(sty, " All rights reserved.\n"); sty_puts(sty, " Type `?' for a list of commands.\n");}static void set_destination(char *dest_name, ipaddr_t *iaddr){ memcpy(&dest_addr, iaddr, sizeof(ipaddr_t)); if (destination != dest_name) { if (destination) etc_strdup_free(destination); destination = etc_strdup(dest_name); } if (destination && !batch_mode) { char *p = SNMP_memory_alloc(STRLEN(destination) + sizeof("snmptalk@> ")); if (p) { STRCPY(p, "snmptalk@"); strcat(p, destination); strcat(p, "> "); if (prompt) free(prompt); prompt = p; } }}/* * Second step routine for setup. Split because setting destination might * have to be asynchronous. */static void setup2(void){ /* * if there is a command on the command line, run it now, * else if there's a default command in the enviroment, run it now, * otherwise go into interactive mode */ if (setup_args.argc) { quit_flag = 1; if (command_run(cty, setup_args.argc, setup_args.argv)) command_next(cty); } else if (setup_args.envc > 0) { quit_flag = 1; if (command_run(cty, setup_args.envc, setup_args.envv)) command_next(cty); } else { if (!batch_mode) { banner(cty); if (!prompt) prompt = etc_strdup("snmptalk> "); } command_next(cty); }}/* Was able to translate the address on the command line. */static void setup1(struct sty *sty, char *dest_name, ipaddr_t *iaddr){ if (iaddr == 0) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "Can not locate host %s\n", dest_name); snark_exit(1); } else { set_destination(dest_name, iaddr); setup2(); }}/* * Parse environment and command line and execute first command if any. * Returns 1 if there was no command found and should go into interactive * mode. Returns 0 after executing a command from either the command line * or the environment. The program should not exit until command_next() * is called so to let the command complete. */void snark_main(struct task *task, void *cookie){ struct snark_main_args *main_args = cookie; int envc, env_opt_count, opt_count, argc; char **argv, *env, *envv[30]; sscanf(snmptalk_version_string+1, "Revision: %d.%d", &snmptalk_major_version, &snmptalk_minor_version);#if INSTALL_ATTACHE snmp_product_id[PRODUCT_ID_MAJOR_VERSION_INDEX] = snmptalk_major_version; snmp_product_id[PRODUCT_ID_MINOR_VERSION_INDEX] = snmptalk_minor_version;#endif /* read defaults out of the config file */ if (SNMP_Config()) { snark_exit(1); return; } /* parse options out of enviroment variable */ if ((env = getenv(ETC_ENV_VARIABLE)) != 0) { envc = parse_line(env, envv, sizeof(envv)/sizeof(char *)); env_opt_count = SNMP_Parse_Opts(envc, envv, "environment variable"); if (env_opt_count < 0) { snark_exit(1); return; } } else { envc = env_opt_count = 0; } /* parse options out of command line */ argc = main_args->argc - 1; argv = main_args->argv + 1; /* skip command name */ opt_count = SNMP_Parse_Opts(argc, argv, "command line"); if (opt_count < 0) { snark_exit(1); return; } argc -= opt_count; argv += opt_count; setup_args.argc = argc; setup_args.argv = argv; setup_args.envc = envc - env_opt_count; setup_args.envv = envv + env_opt_count; /* read in mib for manager */ if (read_mib(mib_filename) == 0) { snark_exit(1); return; } /* initialize the agentx stuff if it's installed */#if INSTALL_ENVOY_AGENTX_MASTER agentx_init();#endif /* translate host name to address */ if (destination) {#if INSTALL_SNMP_DEMO_AGENT_IPV6 /* Assume an IPv6 address if we find ':' */ if (strchr (destination, ':') != NULL) { char * pScope; /* Check if the cope ID is also passed, and save it if so. */ if ((strtok (destination, "%") != NULL) && ((pScope = strtok (NULL, "%")) != NULL)) v6_scope = strtoul (pScope, NULL, 0); md_hostname_to_v6_addr(cty, destination, setup1); } else#endif /* INSTALL_SNMP_DEMO_AGENT_IPV6 */ md_hostname_to_addr(cty, destination, setup1); } else setup2();}/* * Command functions *//* exit the program */boolean_t do_quit(struct sty *sty, enum help_level help, int argc, char *argv[]){ switch (help) { case help_short: sty_puts(sty, "quit - quit the snmptalk program\n"); break; case help_long: sty_puts(sty, "The 'quit' command exits from the snmptalk program.\n"); break; case help_none: quit_flag = 1; break; } return (1);}/* print a short list of commands */boolean_t do_short_help(struct sty *sty, enum help_level help, int argc, char *argv[]){ struct cmd *cmd; switch (help) { case help_short: sty_puts(sty, "? - short listing of commands\n"); break; case help_long: sty_puts(sty, "\The '?' command gives a short listing of all the available commands.\n"); break; case help_none: for (cmd = &commands[0]; cmd->name; cmd++) cmd->rtn(sty, help_short, 0, 0); } return (1);}/* print information on the command given as an argument */static char help_help_long_msg[] = "\Use `help' with a command name to find out more about that command.\n\Configuration is picked up from a configuration file, from an environment\n\variable, from the command line, and by interactive commands. The config\n\file is found from the environment variable ETC_INI, or in a file named\n\etc.ini in the local directory or in /etc in that order. Type `help config'\n\for more information on what's in the config file. The enviroment\n\variable ETC_SNMP_CONF is read and parsed as a command line and any options\n\found are processed. Then the command line is parsed and any options found\n\there are executed overriding the defaults from the enviroment. Then the\n\command from the environment if any is executed unless there's a command on\n\command line in which case it is executed instead. The command line and\n\enviroment variable format is:\n\options [command]\n\Options are:\n\ -d <host> - set the remote host to talk to\n\ -p <udp-port> - set the UDP port to use\n\ -r <read-community> - set the community string to use for reads\n\ -w <write-community> - set the community string to use for writes\n\ -t <seconds> - set the retransmission timeout\n\ -c <count> - set the retransmission count\n\ -m <mib-file> - set the name of the mib file\n\ -a <0/1> - set the display mode - 0 is default\n\Command is any interactive command followed by its arguments.\n";static char help_config_format[] = "\From configuration file:\[etc envoy]\n\; name of the readtree format file holding mib information for SNMPTalk.\n\mibfile=%s\n\; community string for gets and get-nexts\n\read-community=%s\n\; community string for sets\n\write-community=%s\n\; The number of seconds before retransmitting.\n\timeout=%d\n\; The number of times to retransmit an SNMP request.\n\retry-count=%d\n\;The UDP port to use to communicate with the agent.\n\udp-port=%u\n\; Selects the version of SNMP. Which versions are supported depends on\n\; compile time parameters. Versions supported by this copy of SNMPTalk:\n\; %s\n\snmp-version=%d\n";boolean_t do_help(struct sty *sty, enum help_level help, int argc, char *argv[]){ static char help_word[] = "help"; static char *help_argv[] = { help_word, help_word }; struct cmd *cmd; switch (help) { case help_short: sty_puts(sty, "help <command> - give more information on <command>\n"); break; case help_long: sty_puts(sty, help_help_long_msg); break; case help_none: if (argc == 1) do_help(sty, help_none, 2, help_argv); else { cmd = command_lookup(argv[1]); if (cmd != 0) cmd->rtn(sty, help_long, 0, 0); else if (STRCMP(argv[1], "config") == 0) sty_printf(sty, help_config_format, mib_filename, read_community, write_community, snmp_timeout, retry_count, udp_port,#if INSTALL_ENVOY_SNMP_VERSION_1#if INSTALL_ENVOY_SNMP_VERSION_2 && INSTALL_ENVOY_SNMP_VERSION_3 "1, 2 and 3",#elif INSTALL_ENVOY_SNMP_VERSION_2 "1 and 2",#elif INSTALL_ENVOY_SNMP_VERSION_3 "1 and 3", #else "1", #endif /* v2 and v3 */#else /* INSTALL_ENVOY_SNMP_VERSION_1 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -