📄 snmpint.c
字号:
if (err_msg) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "%s\n", err_msg); return(1); } return(0); }return(1);}/* * Implements the BULK command. */boolean_t do_bulk(struct sty *sty, enum help_level help, int argc, char *argv[]){#if ENVOY_USE_V2_PROTOSSNMP_PKT_T *xmit_pkt;OIDC_T *oid_buf;int *oid_len, nonreps = 0, maxreps = 0, count, i;char *err_msg;if (SNMPTalk_SNMP_Version == 1) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Bulk only available in version 2 or 3\n"); return 1; }switch (help) { case help_short: sty_puts(sty, "\bulk <non-reps> <max-reps> <var> <var> ... - get several MIB variables\n"); break; case help_long: sty_puts(sty, "\The 'bulk <non-reps> <max-reps> <var> <var> ... ' command\n\does an SNMP GET_BULK with non-repeaters = non-reps, \n\maximum repeations = max-reps and from 1 to 20 <vars>.\n"); break; case help_none: if (dest_addr.type == IPNONE) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Need to specify destination first\n"); return 1; } if (argc < 4) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Need to specify MIB variables to BULK\n"); return(1); } nonreps = atoi(argv[1]); maxreps = atoi(argv[2]); argv += 3; argc -= 3; if ((oid_len = (int *)SNMP_memory_alloc(sizeof(int) * argc)) == 0) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Unable to allocate memory for BULK\n"); return(1); } oid_buf = (OIDC_T *)SNMP_memory_alloc(sizeof(OIDC_T) * 40 * argc); if (oid_buf == 0) { if (batch_mode) sty_putc(sty, '|'); SNMP_memory_free((char *)oid_len); sty_puts(sty, "Unable to allocate memory for BULK\n"); return(1); } for (count = 0; argc > 0; argc--, argv++, count++) { oid_len[count] = string2oid(*argv, &oid_buf[count * 40], 40); if (oid_len[count] == 0) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "No mib entry `%s'\n", "bulk" /*argv[1]*/); SNMP_memory_free((char *)oid_len); SNMP_memory_free((char *)oid_buf); return 1; } else if (oid_len[count] > 40) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Internal buffer exceeded, OID too long.\n"); SNMP_memory_free((char *)oid_len); SNMP_memory_free((char *)oid_buf); return 1; } } xmit_pkt = SNMPTalk_Create_Request(GET_BULK_REQUEST_PDU, count, nonreps, maxreps); if (xmit_pkt == 0) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Error creating SNMP request\n"); SNMP_memory_free((char *)oid_len); SNMP_memory_free((char *)oid_buf); return(1); } /* Build the name for an object with the appropriate OID */ for (i = 0; i < count; i++) /* i = index to set */ SNMP_Bind_Null(xmit_pkt, i, oid_len[i], &oid_buf[i * 40]); SNMP_memory_free((char *)oid_len); SNMP_memory_free((char *)oid_buf); err_msg = snmp_send_rec(sty, xmit_pkt, &dest_addr, response_print, error_response); if (err_msg) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "%s\n", err_msg); return(1); } return(0); }return(1);#else /* ENVOY_USE_V2_PROTOS */if (batch_mode) sty_putc(sty, '|');sty_puts(sty, "Do-bulk only compiled for version 2 or 3\n");return(1);#endif /* ENVOY_USE_V2_PROTOS */}static OIDC_T base_oid_buf[40];static OIDC_T *base_oid;static int base_oid_len;static void response_walk(struct sty *sty, SNMP_PKT_T *rcvd_pkt){ SNMP_PKT_T *xmit_pkt; OIDC_T *oid; int oid_len; char *err_msg; if ((rcvd_pkt->pdu.std_pdu.error_status == NO_ERROR) && (rcvd_pkt->pdu.std_pdu.std_vbl.vbl_count != 0)) { oid = rcvd_pkt->pdu.std_pdu.std_vbl.vblist->vb_obj_id.component_list; oid_len = rcvd_pkt->pdu.std_pdu.std_vbl.vblist->vb_obj_id.num_components; if (rcvd_pkt->pdu.std_pdu.std_vbl.vblist->vb_data_flags_n_type == VT_ENDOFMIB) { if (!batch_mode && !alt_display) sty_puts(sty, "No more in MIB\n"); } else if ((oid_len > base_oid_len) && (oidorder(base_oid, oid, base_oid_len) == 0)) { print_pkt(sty, rcvd_pkt); xmit_pkt = SNMPTalk_Create_Request(GET_NEXT_REQUEST_PDU, 1, 0, 0); if (xmit_pkt == 0) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Error creating SNMP request\n"); command_next(sty); return; } /* Build the name for an object with the appropriate OID */ SNMP_Bind_Null(xmit_pkt, /* index to set */ 0, oid_len, oid); SNMP_Free(rcvd_pkt); err_msg = snmp_send_rec(sty, xmit_pkt, &dest_addr, response_walk, error_response); if (err_msg) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "%s\n", err_msg); command_next(sty); } return; } else { if (!batch_mode && !alt_display) sty_puts(sty, "No more in subtree\n"); } } else if (rcvd_pkt->pdu.std_pdu.error_status == NO_SUCH_NAME) { if (!batch_mode && !alt_display) sty_puts(sty, "No more in MIB\n"); } else if ((rcvd_pkt->pdu.std_pdu.error_status == NO_ERROR) && (rcvd_pkt->pdu.std_pdu.std_vbl.vbl_count == 0)) { if (!batch_mode) sty_puts(sty, "No error and no varbinds.\n"); } else { print_snmp_error(sty, rcvd_pkt); } SNMP_Free(rcvd_pkt); command_next(sty);}boolean_t do_walk(struct sty *sty, enum help_level help, int argc, char *argv[]){SNMP_PKT_T *xmit_pkt;OIDC_T *oid;int oid_len;char *err_msg;switch (help) { case help_short: sty_puts(sty, "walk [<sub-tree>] - print a MIB sub-tree\n"); break; case help_long: sty_puts(sty, "\The 'walk [<sub-tree>]' uses SNMP GET-NEXT operators to\n\retrieve a MIB subtree and print it.\n"); break; case help_none: if (dest_addr.type == IPNONE) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Need to specify destination first\n"); return(1); } if (argc > 1) { base_oid_len = string2oid(argv[1], base_oid_buf, sizeof(base_oid_buf)/sizeof(OIDC_T)); if (base_oid_len == 0) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "No mib entry `%s'\n", argv[1]); return(1); } else if (base_oid_len > sizeof(base_oid_buf)/sizeof(OIDC_T)) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Internal buffer exceeded. Starting OID too long.\n"); return(1); } base_oid = base_oid_buf; oid = base_oid_buf; oid_len = base_oid_len; } else { oid = default_base_oid; oid_len = sizeof(default_base_oid)/sizeof(OIDC_T); base_oid = base_oid_buf; base_oid_len = 0; } xmit_pkt = SNMPTalk_Create_Request(GET_NEXT_REQUEST_PDU, 1, 0, 0); if (xmit_pkt == 0) { if (batch_mode) sty_putc(sty, '|'); sty_puts(sty, "Error creating SNMP request\n"); return(1); } /* Build the name for an object with the appropriate OID */ SNMP_Bind_Null(xmit_pkt, /* index to set */ 0, oid_len, oid); err_msg = snmp_send_rec(sty, xmit_pkt, &dest_addr, response_walk, error_response); if (err_msg) { if (batch_mode) sty_putc(sty, '|'); sty_printf(sty, "%s\n", err_msg); return(1); } return(0); }return(1);}/* Sets the snmp version to use or reports the version in use. */boolean_t do_snmp_version(struct sty *sty, enum help_level help, int argc, char *argv[]){int version;switch (help) { case help_short: sty_puts(sty, "version [version] - set the version of SNMP to use\n"); break; case help_long: sty_puts(sty, "\The 'version [version]' command sets the version of SNMP to use\n\(1 or 2). With no argument prints out the version in use.\n"); break; case help_none: if (argc == 1) sty_printf(sty, "SNMP Version %d\n", SNMPTalk_SNMP_Version); else if (argc != 2) sty_printf(sty, "Only one argument to %s command\n", argv[0]); else { version = atoi(argv[1]); switch (version) {#if INSTALL_ENVOY_SNMP_VERSION_1 case 1: SNMPTalk_SNMP_Version = version; break;#endif /* INSTALL_ENVOY_SNMP_VERSION_1 */#if INSTALL_ENVOY_SNMP_VERSION_2 case 2: SNMPTalk_SNMP_Version = version; break;#endif /* INSTALL_ENVOY_SNMP_VERSION_2 */#if INSTALL_ENVOY_SNMP_VERSION_3 case 3: SNMPTalk_SNMP_Version = version; break;#endif#if INSTALL_ENVOY_SNMP_VERSION_USEC case 4: SNMPTalk_SNMP_Version = version; break;#endif default: sty_printf(sty, "Unknown SNMP Version, %d\n", version); break; } } }return(1);}/* This create request will do v1 or v2 as appropriate */SNMP_PKT_T * SNMPTalk_Create_Request(int pdu_type, int vb_count, int nonreps, int maxreps){size_t comm_len;char *comm;switch (SNMPTalk_SNMP_Version) { case 1: if (pdu_type == SET_REQUEST_PDU) { comm_len = STRLEN(write_community); comm = write_community; } else { comm_len = STRLEN(read_community); comm = read_community; } return SNMP_Create_Request2(pdu_type, SNMP_VERSION_1, comm_len, comm, (INT_32_T)req_id++, vb_count, 0, 0); case 2: if (pdu_type == SET_REQUEST_PDU) { comm_len = STRLEN(write_community); comm = write_community; } else { comm_len = STRLEN(read_community); comm = read_community; } return SNMP_Create_Request2(pdu_type, SNMP_VERSION_2, comm_len, comm, (INT_32_T)req_id++, vb_count, nonreps, maxreps);#if INSTALL_ENVOY_SNMP_VERSION_3 case 3: return SNMP_Create_Request_V3(pdu_type, SNMP_VERSION_3, req_id++, 1400, v3_tos_flags, ETC_SEC_MODEL_USM, v3_con_id, v3_con_id_len, (bits8_t *)v3_con_name, STRLEN(v3_con_name), v3_sec_id, v3_sec_id_len, (bits8_t *)v3_sec_name, STRLEN(v3_sec_name), req_id, vb_count, nonreps, maxreps);#endif#if INSTALL_ENVOY_SNMP_VERSION_USEC case 4: return SNMP_Create_Request_USEC(pdu_type, SNMP_VERSION_USEC, 1500, usec_qos, usec_agentid, STRLEN(read_community), (OCTET_T *)read_community, STRLEN(usec_context), usec_context, req_id++, vb_count, nonreps, maxreps);#endif }return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -