首页 > 解决方案 > 使用 snmpSET 和 netsnmp lib 无法设置请求

问题描述

感谢 netsnmp 库,我有一个代理,它对 Getrequest 工作正常,我使用 MibIreasonning 对 get 和 set 进行测试,但是我对 set 有一个大问题,当我做 snmp set 时,我没有响应和值剂量变化,我使用命令行:snmpset -v2c -cprivate 127.0.0.1:10181 1.3.6.1.4.1.4045.61005681.20.2.3.0.0 i 1,我得到:超时:127.0.0.1:10181没有响应

如果我在公共场合做 snmpset,我得到:数据包错误,原因 noAccess

这是我的代码,如果你能帮助我:

//-------------------------------------------------------------------------------------------------
//                         ValueOID
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <iostream>

#include <string>


int handle_AgentSNMP15 (
    netsnmp_mib_handler *handler,
    netsnmp_handler_registration *reginfo,
    netsnmp_agent_request_info *reqinfo,
    netsnmp_request_info *requests) {

    int value=0 ;

    switch (reqinfo->mode) {
        case MODE_GET:
        std::cout<<"GETSNMP15"<<std::endl;
         std::cout<<"get value "<<*(requests->requestvb->val.integer)<<std::endl;
            snmp_set_var_typed_value(
                requests->requestvb,
                ASN_INTEGER,
                (u_char*)&value,
                sizeof(value));
            break;
        case MODE_SET_RESERVE1:
            std::cout<<*(requests->requestvb->val.integer)<<std::endl;
        case MODE_SET_RESERVE2:
            value =  *(requests->requestvb->val.integer);
            std::cout<<"RESERVE2"<<std::endl;
            break;
        case MODE_SET_FREE:
            break;

        case MODE_SET_ACTION:
            std::cout<<"SETACTION"<<std::endl;
            value =  *(requests->requestvb->val.integer);

            break;

        case MODE_SET_COMMIT:
        std::cout<<"SETCOMMIT"<<std::endl;
            value =  *(requests->requestvb->val.integer);
            break;

        case MODE_SET_UNDO:
             break;

        default:
        {
            snmp_log(
                LOG_ERR,
                "Unknown mode (%d) in handle_dot1dBaseType\n",
                reqinfo->mode);
            std::cout<<"ERROR OTHER THAN GET";

            return SNMP_ERR_GENERR;
        }
    }

    return SNMP_ERR_NOERROR;
}

void init_Value() {


    static oid AgentSNMP15[]={1,3,6,1,4,1,4045,61005681,20,2,3,0};



    netsnmp_register_scalar(netsnmp_create_handler_registration(
        "agentSNMP15",
        handle_AgentSNMP15,
        AgentSNMP15,
        OID_LENGTH(AgentSNMP15),
        HANDLER_CAN_RWRITE));



}

谢谢你的帮助

标签: c++snmpnet-snmp

解决方案


推荐阅读