呜呜呜呜呜呜~~~,我太菜了,编了一天!!!
把下面的代码跑起来
#include <iostream>
#include <WinSock2.h>
#define __STDC_LIMIT_MACROS // 添加宏定义
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
int main() {
// 初始化 net-snmp 库
init_snmp("snmpapp");
// 创建 SNMP 会话
netsnmp_session session, *ss;
snmp_sess_init(&session);
session.peername = _strdup("localhost");
session.version = SNMP_VERSION_2c;
session.community = reinterpret_cast<u_char*>("public");
session.community_len = strlen(reinterpret_cast<const char*>(session.community));
ss = snmp_open(&session);
if (!ss) {
std::cerr << "Failed to open SNMP session\n";
return 1;
}
// 构造 OID
oid anOID[MAX_OID_LEN];
size_t anOID_len = MAX_OID_LEN;
const char* oidStr = "1.3.6.1.2.1.2.2.1.8.1"; // 示例 OID
if (!snmp_parse_oid(oidStr, anOID, &anOID_len)) {
std::cerr << "Failed to parse OID\n";
snmp_close(ss);
return 1;
}
// 查询交换机状态
netsnmp_pdu* pdu = snmp_pdu_create(SNMP_MSG_GET);
snmp_add_null_var(pdu, anOID, anOID_len);
netsnmp_pdu* response;
if (snmp_synch_response(ss, pdu, &response) == STAT_SUCCESS) {
if (response->errstat == SNMP_ERR_NOERROR) {
for (netsnmp_variable_list* vars = response->variables; vars; vars = vars->next_variable) {
if (vars->type == ASN_INTEGER) {
std::cout << "Switch status: " << *(vars->val.integer) << std::endl;
}
}
}
else {
std::cerr << "SNMP error: " << snmp_errstring(response->errstat) << std::endl;
}
snmp_free_pdu(response);
}
else {
std::cerr << "Failed to get SNMP response\n";
}
// 释放资源
snmp_close(ss);
return 0;
}
太激动了,终于跑起来了,呜呜呜呜~~~~~~~~~~~~~~~~~~