c - 使用 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 程序中工作?
解决方案
你用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");
请注意它是如何“输入”的,而不是采用字符串进行词法转换。
推荐阅读
- jhipster - 多个集群节点的 JHipster elasticsearch 配置
- mysql - 问题::无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)
- c - CALL指令如何解析成十六进制?
- algolia - 谷歌在 algolia 自动完成上的建议
- ms-access - Access 中的路由时间总和?
- javascript - 如何清除 angular2-datetimepicker 中的值
- javascript - 在数组中查找对象?
- javascript - 在本机反应中获取具有数字键的对象值
- c# - 有没有办法以编程方式将 PM2 连接到 C# Windows 服务
- azure-data-factory - 如果发生错误,如何暂停数据工厂翻转窗口触发