snmp v3 获取节点信息

#include "net-snmp/net-snmp-config.h"
#include "net-snmp/net-snmp-includes.h"
#include "net-snmp/utilities.h"
#include "net-snmp/agent/net-snmp-agent-includes.h"
#include "net-snmp/agent/agent_callbacks.h"
#include "net-snmp/library/large_fd_set.h"


#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>

using namespace std;

typedef struct oid_name_val
{
	std::string oidname;
	std::string oidvalue;
	oid_name_val() :oidname(""),
		oidvalue("")
	{}
}oid_name_val;


void ParseVariable(netsnmp_variable_list* vars)
{
	std::vector<oid_name_val> oidresult;
	
	std::string::size_type pos1, pos2, pos3, pos4;
	std::string oidname;
	std::string oidvalue;

	oid_name_val oidinfo;
	size_t buf_len = 1024;
	size_t out_len = 0;
	u_char buf[1024];
	memset(buf, 0, sizeof(buf));
	u_char *pBuf = buf;

	if (sprint_realloc_variable(&pBuf, &buf_len, &out_len, 1, vars->name, vars->name_length, vars))
	{
		std::string strTemp = (char*)buf;
		pos1 = strTemp.find("=");
		if (pos1 == strTemp.npos)
		{
			return;
		}

		oidname = strTemp.substr(0, pos1 - 1); //取得OID

		pos2 = strTemp.find(":", pos1);
		if (pos2 == strTemp.npos)
		{
			oidvalue = ""; //没有OID相对应的值
		}

		else
		{
			pos3 = strTemp.find("\"", pos2 + 1);
			if (pos3 != strTemp.npos)
			{
				pos4 = strTemp.find("\"", pos3 + 1);
				if (pos4 != strTemp.npos)
				{
					oidvalue = strTemp.substr(pos3 + 1, pos4 - (pos3 + 1)); //取得OID相对应的值
				}
			}
			else
			{
				oidvalue = strTemp.substr(pos2 + 2);
			}
		}
	}
	else
	{
		fprintf(stderr, "%s [TRUNCATED]\n", buf);
	}

	oidinfo.oidname = oidname.c_str();
	oidinfo.oidvalue = oidvalue.c_str();

	oidresult.push_back(oidinfo);
	
	std::vector<oid_name_val>::iterator iter;
	for (iter = oidresult.begin(); iter != oidresult.end(); iter++)
	{
		string strText = iter->oidname + " = " + iter->oidvalue + "\n";
		printf(strText.c_str() );
	}
}
int main()
{
	
	//初始化SNMP及agent库
	init_snmp("snmpapp");


	//oid以数字形式输出
	netsnmp_ds_set_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OID_OUTPUT_FORMAT,
		NETSNMP_OID_OUTPUT_NUMERIC);
	
	netsnmp_session session;
	//初始化并设置session值
	snmp_sess_init(&session);

	session.timeout = 5 * 1000000;

	char agenturl[64] = { 0 };
	snprintf(agenturl, 63, "%s:%d", "10.14.68.32", 161);
	session.peername = agenturl;

	session.version = SNMP_VERSION_3;
	session.securityName = strdup("admin");
	session.securityNameLen = strlen(session.securityName);
	/*
		#define SNMP_SEC_LEVEL_NOAUTH		1
		#define SNMP_SEC_LEVEL_AUTHNOPRIV	2
		#define SNMP_SEC_LEVEL_AUTHPRIV		3
	*/
	session.securityLevel = SNMP_SEC_LEVEL_AUTHPRIV;

	session.securityAuthProto = usmHMACMD5AuthProtocol;
	session.securityAuthProtoLen = USM_AUTH_PROTO_MD5_LEN;
	session.securityAuthKeyLen = USM_AUTH_KU_LEN;

	if (generate_Ku(session.securityAuthProto, session.securityAuthProtoLen, (u_char *)("admin12345"), 10, session.securityAuthKey, &session.securityAuthKeyLen) != SNMPERR_SUCCESS)
	{
		printf("Error generating Ku from authentication pass phrase. /n");
		exit(1);
	}

	session.securityPrivProto = usmDESPrivProtocol;
    session.securityPrivProtoLen = USM_PRIV_PROTO_DES_LEN;
	session.securityPrivKeyLen = USM_PRIV_KU_LEN;

	 if (generate_Ku(session.securityAuthProto,session.securityAuthProtoLen,(u_char *) ("admin12345"), 10, session.securityPrivKey, &session.securityPrivKeyLen) != SNMPERR_SUCCESS)
	 {
		printf("Error generating Ku from authentication pass phrase. /n");
		return -1;
	}

	netsnmp_session *slp = NULL;
	//打开一个session
	slp = snmp_open(&session);
	if (NULL == slp)
	{
		printf("open a snmp session error\n");
		return 0 ;
	}
	
	netsnmp_pdu *pdu = NULL;
	netsnmp_pdu *response = NULL;
	netsnmp_variable_list *vars = NULL;
	int status = 0;

	//创建PDU,并添加OID
	pdu = snmp_pdu_create(SNMP_MSG_GET);
	if (NULL == pdu)
	{
		printf("snmp_pdu_create error\n");
		return 0;
	}

	size_t name_length = MAX_OID_LEN;
	oid             name[MAX_OID_LEN];
	if (!snmp_parse_oid(".1.3.6.1.4.1.39165.1.19.0", name, &name_length))
	{
		snmp_perror(".1.3.6.1.4.1.39165.1.19.0 \n");
		printf("snmp_parse_oid error");
		return 0;
	}
	else
	{
		snmp_add_null_var(pdu, name, name_length);
	}

	//同步获取信息
	status = snmp_synch_response(slp, pdu, &response);
	if (STAT_SUCCESS == status)
	{
		if (NULL == response)
		{
			printf("snmp_synch_response, response is NULL \n");
			return 0;
		}

		if (response->errstat == SNMP_ERR_NOERROR)
		{
			for (vars = response->variables; vars; vars = vars->next_variable)
			{
				ParseVariable(vars);             //解析variables
			}
		}
		else
		{
			printf("snmp_sess_synch_response error \n");
			return 0;
		}
		snmp_free_pdu(response);
	}

	else if (STAT_TIMEOUT == status)
	{
		printf("Timeout: No Response \n");
		snmp_free_pdu(response);
		return 0;
	}
	else if (STAT_ERROR == status)
	{
		printf("snmp_synch_response error \n");
		snmp_free_pdu(response);
		return 0;
	}
	
	
	return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值