.net,实现snmp协议与c

发布时间:2016-11-27 来源: 协议 点击:

篇一:net-snmp agent c开发

Net-snmp 使用c 扩展 agent

摘要:

netSNMP开发,使用c开发对SNMP进行扩展,应用程序作为agent执行。 官网:http://www.net-snmp.org

版本:NET-SNMP version 5.7.2.1

系统:CentOS X64

软件包:net-snmp-5.7.2.1.tar.gz

解压安装包:

[root@localhost snmp]# pwd

/root/snmp

[root@localhost snmp]# ls

net-snmp-5.7.2.1.tar.gz

[root@localhost snmp]# tar xzvf net-snmp-5.7.2.1.tar.gz

[root@localhost snmp]# ls

net-snmp-5.7.2.1 net-snmp-5.7.2.1.tar.gz

配置

[root@localhost snmp]# cd net-snmp-5.7.2.1

[root@localhost net-snmp-5.7.2.1]# pwd

/root/snmp/net-snmp-5.7.2.1

[root@localhost net-snmp-5.7.2.1]# ./configure

配置完成后,会显示如下configure摘要信息

---------------------------------------------------------

Net-SNMP configuration summary:

---------------------------------------------------------

SNMP Versions Supported: 1 2c 3

Building for:linux

Net-SNMP Version: 5.7.2.1

Network transport support: Callback Unix Alias TCP UDP IPv4Base SocketBase TCPBase UDPIPv4Base UDPBase

SNMPv3 Security Modules: usm

Agent MIB code: default_modules => snmpv3mibs mibII ucd_snmp notification notification-log-mib target agent_mibs agentx disman/event disman/schedule utilities host

MYSQL Trap Logging:unavailable

Embedded Perl support:enabled

SNMP Perl modules: building -- embeddable

SNMP Python modules: disabled

Crypto support from: crypto

Authentication support: MD5 SHA1

Encryption support:DES AES

Local DNSSEC validation: disabled

编译、安装

make && make install

配置snmpd.conf

[root@localhost snmp]# pwd

/usr/local/share/snmp

[root@localhost snmp]# snmpconf

The following installed configuration files were found:

1: ./snmpd.conf

2: ./snmptrapd.conf

3: /usr/local/share/snmp/snmpd.conf

4: /usr/local/share/snmp/snmptrapd.conf

Would you like me to read them in? Their content will be merged with the

output files created by this session.

Valid answer examples: "all", "none","3","1,2,5"

Read in which (default = all): none

I can create the following types of configuration files for you.

Select the file type you wish to create:

(you can create more than one as you run this program)

1: snmpd.conf

2: snmptrapd.conf

3: snmp.conf

Other options: quit

Select File: 1

The configuration information which can be put into snmpd.conf is divided

into sections. Select a configuration section for snmpd.conf

that you wish to create:

1: Access Control Setup

2: Extending the Agent

3: Trap Destinations

4: Monitor Various Aspects of the Running Host

5: Agent Operating Mode

6: System Information Setup

Other options: finished

Select section: 5

Section: Agent Operating Mode

Description:

This section defines how the agent will operate when it

is running.

Select from:

1: Should the agent operate as a master agent or not.

2: The system user that the agent runs as.

3: The system group that the agent runs as.

4: The IP address and port number that the agent will listen on.

Other options: finished, list

Select section: 1

Configuring: master

Description:

Should the agent operate as a master agent or not.

Currently, the only supported master agent type for this token

is "agentx".

arguments: (on|yes|agentx|all|off|no)

Should the agent run as a AgentX master agent?: agentx

Finished Output: master agentx

Section: Agent Operating Mode

Description:

This section defines how the agent will operate when it

is running.

Select from:

1: Should the agent operate as a master agent or not.

2: The system user that the agent runs as.

3: The system group that the agent runs as.

4: The IP address and port number that the agent will listen on.

Other options: finished, list

Select section: finished

The configuration information which can be put into snmpd.conf is divided

into sections. Select a configuration section for snmpd.conf

that you wish to create:

1: Access Control Setup

2: Extending the Agent

3: Trap Destinations

4: Monitor Various Aspects of the Running Host

5: Agent Operating Mode

6: System Information Setup

Other options: finished

Select section: 1

Section: Access Control Setup

Description:

This section defines who is allowed to talk to your running

snmp agent.

Select from:

1: a SNMPv3 read-write user

2: a SNMPv3 read-only user

3: a SNMPv1/SNMPv2c read-only access community name

4: a SNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: 3

Configuring: rocommunity

Description:

a SNMPv1/SNMPv2c read-only access community name

arguments: community [default|hostname|network/bits] [oid]

The community name to add read-only access for: public

The hostname or network address to accept this community name from [RETURN for all]: The OID that this community should be restricted to [RETURN for no-restriction]:

Finished Output: rocommunity public

Section: Access Control Setup

Description:

This section defines who is allowed to talk to your running

snmp agent.

Select from:

1: a SNMPv3 read-write user

2: a SNMPv3 read-only user

3: a SNMPv1/SNMPv2c read-only access community name

4: a SNMPv1/SNMPv2c read-write access community name

Other options: finished, list

Select section: 4

Configuring: rwcommunity

Description:

a SNMPv1/SNMPv2c read-write access community name

arguments: community [default|hostname|network/bits] [oid]

Enter the community name to add read-write access for: private

The hostname or network address to

.net 实现snmp协议与c

accept this community name from [RETURN for all]:

篇二:net-snmp配置开发及注意事项

net-snmp配置开发及注意事项

分类: 网络 2010-03-11 13:29 1781人阅读 评论(1) 收藏 举报

一.安装及配置 SNMP ( win32 环境)

1. 下载

从 下载到最新的 net-snmp (目前最新版本 5.4.1 ) net-snmp-5.4.1.zip

2. 解压编译

解压后,可以看到有一个 win32 目录,里面存放的是和 win32 环境相关的文件,有 3 个 dsw :

libsdll.dsw 编译 lib 文件和 dll 文件的工程

win32.dsw编译 lib 文件和工具文件如 snmpget,snmpset 的工程

win32sdk.dsw 类似于 win32.dsw ,区别在于:需要安装 Platform SDK 。如果需要 agent 能支持 interfaces 等一些高级功能,必须用此工程编译。 XPSP2 Platform SDK 的下载地址

/msdownload/platformsdk/sdkupdate/XPSP2FULLInstall.htm

只需要安装 Core SDK 就可以了,安装完后需要从开始菜单中 Register 一下。

注意编译的顺序,最好先编译 libsdll.dsw ,把 netsnmp.lib , netsnmpagent.lib , netsnmphelpers.lib , netsnmpmibs.lib , netsnmptrapd.lib 文件先编译好,再编译 win32sdk.dsw 中的项目。

3. 安装

运行 win32 目录下的 install-net-snmp.bat 批处理文件,会把上一步编译生成的文件及相关的头文件等拷贝到 c:/usr 目录。

4. 配置

在 c:/usr/etc/snmp 目录添加配置文件 snmpd.conf ,添加如下内容:

rocommunity public

rwcommunity private

它表示的含义是,启动 agent 服务后,通过 public 共同体是只读的, private 共同体可读也可写。

在命令行运行如下命令,将 snmp 注册为 windows 的服务: cmd>”C:/usr/bin/snmpd.exe” –register -Lf

"C:/usr/log/snmpd.log"

注册成功后可以在【控制面板】 -> 【管理工具】 -> 【服务】中看到刚注册的服务,服务名是: net-snmp agent

5. 运行

cmd>net start “net-snmp agent”

如果正常,会得到启动服务成功的提示

6. 验证

cmd>snmpget –v2c –c public localhost 1.3.6.1.2.1.1.5.0

cmd> snmpset -v2c -c private localhost sysContact.0 = piyeyong如果正常,会的到取得和设置成功的提示,出错会给出错误提示。

二. MIB 文件编写

MIB 文件会存放于 C:/usr/share/snmp/mibs/ 目录下,是 *.txt ,纯文本文件,可以直接打开查看和更改。 RFC1213 中定义的 MIB 节点信息的定义存放与 RFC1213-MIB.txt ,这些节点是比较重要的,会经常用到。

如果要扩展 MIB ,应该定义在

1.3.6 .1.4.1(.iso.org.dod.internet.private.enterprises) 子树下。自定义 MIB 的节点,只需要描述该节点的 SYNTAX , ACCESS , STATUS , DESCRIPTION 等属性及它属于父节点的第几个子节点即可。如下所示,为扩展 MIB 的一个简单例子:

PROBA-MIB DEFINITIONS::=BEGIN

IMPORTS

enterprises,OBJECT-TYPE,Integer32,TimeTicks

FROM SNMPv2-SMI

TEXTUAL-CONVENTION, DisplayString FROM SNMPv2-TC;

-- proba node

proba OBJECT IDENTIFIER::={enterprises 8888}

baseinfo OBJECT IDENTIFIER ::= { proba 1 }

-- company name

probaCompName OBJECT-TYPE

SYNTAX DisplayString (SIZE (0..255))

ACCESS read-only

STATUS mandatory

DESCRIPTION "The Name of company"

::={baseinfo 1}

-- company location

probaLocation OBJECT-TYPE

SYNTAX DisplayString (SIZE (0..255))

ACCESS read-write

STATUS mandatory

DESCRIPTION "The Location of company"

::={baseinfo 2}

-- employee number

probaEmployeeNumber OBJECT-TYPE

SYNTAX INTEGER

ACCESS read-only

STATUS mandatory

DESCRIPTION "The number of employee"

::={baseinfo 3}

END

三. Agent 端开发

在上一步中定义好 MIB 的结构后,现在就开始编码实现定义好的节点。 net-snmp 提供了一个 MIB2C 工具,利用它可以根据 MIB 的定义和配置文件自动生成 *.c 和 *.h 模板文件,然后只需要在相应位置添加对节点数据的处理就可以了。

1. 配置 net-snmp 的 perl 模块

用使用 mib2c 工具,需要 perl 模块的支持,可以从

/ActivePerl 下载,目前最新版是 5.8.8在 net-snmp 源文件的 perl 目录下,运行以下命令:

cmd>perl makefile.pl

如果成功,会生成 makefile 文件

cmd>nmake

cmd>nmake install

这时,会将 net-snmp 相关的 perl 模块编译好并安装到

c:/perl/site/lib 目录下。

注:有时候运行 nmake 会失败,把其它机器上安装好的

c:/perl/site/lib 目录下的文件拷贝过来,也可以运行。

2. 用 mib2c 生成模板源代码

运行以下命令:

cmd>mib2c -c mib2c.scalar.conf baseinfo

会按照模板配置文件 mib2c.scalar.conf 生成 baseinfo.h 和

baseinfo.c 文件。注意: baseinfo 是上一步在 MIB 中定义的 proba 下的一个节点。在 baseinfo.c 中有很多 /* XXX 注释 */ 的地方,这些地方是需要我们修改,填上我们对节点数据的处理代码。

3. 对 read-only 节点的代码修改

以 probaCompName 节点为例:

int

handle_probaCompName(netsnmp_mib_handler *handler,

netsnmp_handler_registration *reginfo, netsnmp_agent_request_info*reqinfo,

netsnmp_request_info*requests) {

/* We are never called for a GETNEXT if it's registered as a"instance", as it's "magically" handled for us. */

/* a instance handler also only hands us one request at a time, sowe don't need to loop over a list of requests; we'll only get one. */

switch (reqinfo->mode) {

case MODE_GET:

snmp_set_var_typed_value(requests->requestvb,

ASN_OCTET_STR,

(u_char *) "proba" /* XXX: a pointer to the scalar's data */ ,

strlen( "proba" ) /* XXX: the length of the data in bytes */ );

break ;

default :

/* we should never get here, so this is a really bad error */

snmp_log(LOG_ERR, "unknown mode (%d) in

handle_probaCompName/n" , reqinfo->mode );

return SNMP_ERR_GENERR;

}

return SNMP_ERR_NOERROR;

}

从上面的代码看出,只需在两处 /* XXX 注释 */ 的代码处填上这个节点的数据即可,管理站在执行 get 命令时这个值会返回给管理站。

4. 对 read-write 节点的代码修改

篇三:简单网络协议SNMP之net

简单网络协议SNMP之net-snmp开发snmpget

分类: Ivan@_OpenSource 2012-02-17 17:04 1437人阅读 评论(0) 收藏 举报

网络协议sessionincludestringnullcompiler

目的:通过net-snmp关于snmpget源码的开发,从snmp代理设备获得自己想要的信息 准备:从http://www.net-snmp.org/网址下载net-snmp-5.6.1.1.tar.gz源码包

开发过程:

1.解压源码

2.进入解压源码的目录

1) Run ./configure --prefix=/usr/local/net-snmp

(type "./configure --help" for a quick usage summary.)

(--prefix=PATH will change the default /usr/local installation path.)

(see "Compilers and Options" on changing the compiler to use)

2) make

3) make install

3.安装完毕

在/usr/local/net-snmp底下内容如下:

root@an-virtual-machine:/usr/local/net-snmp# ls

bin include lib sbin share

进入lib,可以看到内容如下:

root@an-virtual-machine:/usr/local/net-snmp/lib# ls

libnetsnmp.a libnetsnmpmibs.la

libnetsnmpagent.alibnetsnmpmibs.so

libnetsnmpagent.la libnetsnmpmibs.so.25

libnetsnmpagent.so libnetsnmpmibs.so.25.0.2

libnetsnmpagent.so.25 libnetsnmp.so

libnetsnmpagent.so.25.0.2 libnetsnmp.so.25

libnetsnmphelpers.a libnetsnmp.so.25.0.2

libnetsnmphelpers.lalibnetsnmptrapd.a

libnetsnmphelpers.solibnetsnmptrapd.la

libnetsnmphelpers.so.25libnetsnmptrapd.so

libnetsnmphelpers.so.25.0.2 libnetsnmptrapd.so.25

libnetsnmp.la libnetsnmptrapd.so.25.0.2

libnetsnmpmibs.a

4.在Qtcreator,新建一个项目TestNetSNMp

把上面lib里面的libnetsnmp.a 放到项目底下

在.pro里面添加内容如下:

INCLUDEPATH +=/usr/local/net-snmp/include

LIBS += -L/root/wyz/TestNetSNMP -lnetsnmp

5.在main里面添加代码如下:

[cpp] view plaincopyprint?

1. #include <net-snmp/net-snmp-config.h>

2.

3. #if HAVE_STDLIB_H

4. #include <stdlib.h>

5. #endif

6. #if HAVE_UNISTD_H

7. #include <unistd.h>

8. #endif

9. #if HAVE_STRING_H

10. #include <string.h>

11. #else

12. #include <strings.h>

13. #endif

14. #include <sys/types.h>

15. #if HAVE_NETINET_IN_H

16. #include <netinet/in.h>

17. #endif

18. #include <stdio.h>

19. #include <ctype.h>

20. #if TIME_WITH_SYS_TIME

21. # ifdef WIN32

22. # include <sys/timeb.h>

23. # else

24. # include <sys/time.h>

25. # endif

26. # include <time.h>

27. #else

28. # if HAVE_SYS_TIME_H

29. # include <sys/time.h>

30. # else

31. # include <time.h>

32. # endif

33. #endif

34. #if HAVE_SYS_SELECT_H

35. #include <sys/select.h>

36. #endif

37. #if HAVE_WINSOCK_H

38. #include <winsock.h>

39. #endif

40. #if HAVE_NETDB_H

41. #include <netdb.h>

42. #endif

43. #if HAVE_ARPA_INET_H

44. #include <arpa/inet.h>

45. #endif

46.

47. #include <net-snmp/utilities.h>

49. #include <net-snmp/net-snmp-includes.h>

50. #include <vector>

51. #include <map>

52. #include <iostream>

53. using namespace std;

54.

55. #define NETSNMP_DS_APP_DONT_FIX_PDUS 0

56.

57.

58.

59. string fprint_variable_1(const oid * objid,size_t objidlen, const netsnmp_variab

le_list * variable)

60. {

61. u_char*buf = NULL;

62. size_t buf_len = 256, out_len = 0;

63. if ((buf = (u_char *) calloc(buf_len, 1)) == NULL) {

64.

65.return "";

66. } else {

67.if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,

68. objid, objidlen, variable)) {

69. string strTemp((char*)buf);

70.

71. int iFirst=strTemp.find_first_of(":");

72. string strInfo=strTemp.substr(iFirst,strTemp.length()-1);

73.

74. return strInfo;

75.

76.} else {

77. fprintf(stdout, "%s [TRUNCATED]\n", buf);

78.}

79. }

80. SNMP_FREE(buf);

81. }

83. string fprint_variable_2(const oid * objid,size_t objidlen, const netsnmp_variab

le_list * variable)

84. {

85. u_char*buf = NULL;

86. size_t buf_len = 256, out_len = 0;

87. if ((buf = (u_char *) calloc(buf_len, 1)) == NULL) {

88.

89.return "";

90. } else {

91.if (sprint_realloc_variable(&buf, &buf_len, &out_len, 1,

92. objid, objidlen, variable)) {

93. string strTemp((char*)buf);

94.

95. return strTemp;

96.

97.} else {

98. fprintf(stdout, "%s [TRUNCATED]\n", buf);

99.}

100. }

101. SNMP_FREE(buf);

102. }

103.

104. string SnmpGet(char *community,char *ip,char *oid_array)//snmpget -v2c -

c public 10.0.0.11 1.3.6.1.2.1.1.5.0

105. {

106. netsnmp_session session, *ss=(netsnmp_session*)malloc(sizeof(netsnm

p_session));

107. netsnmp_pdu *response=(netsnmp_pdu*)malloc(sizeof(netsnmp_pdu))

;

108.

109. netsnmp_variable_list *vars=NULL;

110. netsnmp_pdu *pdu;

111.

112. if(!ss)

相关热词搜索:协议 net snmp 让snmp包通过防火墙 思科防火墙snmp 网页版的snmp

热点文章阅读

版权所有 小龙文挡网 www.xltkwj.com