首页 > 解决方案 > 使用 net-snmp C API 设置值时出现问题

问题描述

以下是相关代码:

pdu = snmp_pdu_create(SNMP_MSG_SET);

if (snmp_add_var(pdu, oid, oid_len, ASN_INTEGER, "1" ) != 0 )
    snmp_perror("failed");

我收到错误“错误的值类型:奇怪的 Unicode 字符

当我在终端中运行这个 snmpset 命令时:

snmpset -v 3 -u <user> <ip> <oid> integer 1

它工作正常,为什么它不能在我的 C 程序中工作?

标签: cnetwork-programmingsnmpnet-snmp

解决方案


你用snmp_add_var错了。它在某种意义上相当于 of snmpset,所以你应该传递字符'i'而不是常量ASN_INTEGER,它是为其他东西设计的。

ASN_INTEGER被定义为((u_char)0x02),因此导致解码困难的是对该参数的解析。


“其他东西”是您可能想要使用的功能,即snmp_pdu_add_variable

pdu = snmp_pdu_create(SNMP_MSG_SET);

uint32_t val = 1;
if (snmp_pdu_add_variable(pdu, oid, oid_len, ASN_INTEGER, &val, sizeof(val)) == nullptr)
    snmp_perror("failed");

请注意它是如何“输入”的,而不是采用字符串进行词法转换。


推荐阅读