简单网络协议SNMP之net.docx

上传人:b****7 文档编号:8744555 上传时间:2023-02-01 格式:DOCX 页数:18 大小:20KB
下载 相关 举报
简单网络协议SNMP之net.docx_第1页
第1页 / 共18页
简单网络协议SNMP之net.docx_第2页
第2页 / 共18页
简单网络协议SNMP之net.docx_第3页
第3页 / 共18页
简单网络协议SNMP之net.docx_第4页
第4页 / 共18页
简单网络协议SNMP之net.docx_第5页
第5页 / 共18页
点击查看更多>>
下载资源
资源描述

简单网络协议SNMP之net.docx

《简单网络协议SNMP之net.docx》由会员分享,可在线阅读,更多相关《简单网络协议SNMP之net.docx(18页珍藏版)》请在冰豆网上搜索。

简单网络协议SNMP之net.docx

简单网络协议SNMP之net

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

分类:

Ivan@_OpenSource2012-02-1717:

041437人阅读评论(0)收藏举报

网络协议sessionincludestringnullcompiler

目的:

通过net-snmp关于snmpget源码的开发,从snmp代理设备获得自己想要的信息

准备:

从-snmp.org/网址下载net-snmp-5.6.1.1.tar.gz源码包

 

开发过程:

1.解压源码

2.进入解压源码的目录

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

    (type"./configure--help"foraquickusagesummary.)

    (--prefix=PATHwillchangethedefault/usr/localinstallationpath.)

    (see"CompilersandOptions"onchangingthecompilertouse)

 2)make

 3)makeinstall

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.a           libnetsnmpmibs.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.la        libnetsnmptrapd.a

libnetsnmphelpers.so        libnetsnmptrapd.la

libnetsnmphelpers.so.25     libnetsnmptrapd.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]viewplaincopyprint?

1.#include   

2.  

3.#if HAVE_STDLIB_H  

4.#include   

5.#endif  

6.#if HAVE_UNISTD_H  

7.#include   

8.#endif  

9.#if HAVE_STRING_H  

10.#include   

11.#else  

12.#include   

13.#endif  

14.#include   

15.#if HAVE_NETINET_IN_H  

16.#include   

17.#endif  

18.#include   

19.#include   

20.#if TIME_WITH_SYS_TIME  

21.# ifdef WIN32  

22.#  include   

23.# else  

24.#  include   

25.# endif  

26.# include   

27.#else  

28.# if HAVE_SYS_TIME_H  

29.#  include   

30.# else  

31.#  include   

32.# endif  

33.#endif  

34.#if HAVE_SYS_SELECT_H  

35.#include   

36.#endif  

37.#if HAVE_WINSOCK_H  

38.#include   

39.#endif  

40.#if HAVE_NETDB_H  

41.#include   

42.#endif  

43.#if HAVE_ARPA_INET_H  

44.#include   

45.#endif  

46.  

47.#include   

48.  

49.#include   

50.#include   

51.#include   

52.#include   

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_variable_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.}  

82.  

83.string fprint_variable_2(const oid * objid,size_t objidlen, const netsnmp_variable_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(netsnmp_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)  

113.        ss = (netsnmp_session*)malloc(sizeof(netsnmp_session));  

114.    if(!

response)  

115.        response=(netsnmp_pdu*)malloc(sizeof(netsnmp_pdu));  

116.      

117.      

118.    int             count;  

119.    int             current_name = 0;  

120.    char           *names[SNMP_MAX_CMDLINE_OIDS];  

121.    oid             name[MAX_OID_LEN];  

122.    size_t          name_length;  

123.    int             status;  

124.    int             failures = 0;  

125.    int             exitval = 0;  

126.      

127.    snmp_sess_init(&session);  

128.      

129.    session.version = SNMP_VERSION_2c;  

130.      

131.    session.peername = ip;  

132.    munity = (unsigned char*)community;  

133.    munity_len = strlen(community);  

134.      

135.      

136.    names[0] = oid_array;  

137.    current_name = 1;  

138.      

139.    SOCK_STARTUP;  

140.      

141.    snmp_close(ss);  

142.    ss = snmp_open(&session);  

143.      

144.    if (ss == NULL) {  

145.        snmp_sess_perror("snmpget", &session);  

146.        SOCK_CLEANUP;  

147.        return "";  

148.    }  

149.      

150.    pdu = snmp_pdu_create(SNMP_MSG_GET);  

151.    for (count = 0; count < current_name; count++) {  

152.        name_length = MAX_OID_LEN;  

153.        if (!

snmp_parse_oid(names[count], name, &name_length)) {  

154.            snmp_perror(names[count]);  

155.            failures++;  

156.        }  

157.        else  

158.        {  

159.            snmp_add_null_var(pdu, name, name_length);  

160.        }  

161.    }  

162.    if (failures) {  

163.        snmp_close(ss);  

164.        SOCK_CLEANUP;  

165.        return "";  

166.    }  

167.      

168.    retry:

  

169.    status = snmp_synch_response(ss, pdu, &response);  

170.    if (status == STAT_SUCCESS) {  

171.        if (response->errstat == SNMP_ERR_NOERROR)  

172.        {     

173.            vars = response->variables;  

174.              

175.            string strTemp=fprint_variable_1(vars->name, vars->name_length, vars);  

176.              

177.            return strTemp;  

178.        }  

179.        else  

180.        {  

181.            printf("2\n");  

182.            fprintf(stderr, "Error in packet\nReason:

 %s\n",  

183.                    snmp_errstring(response->errstat));  

184.              

185.            if (response->errindex !

= 0) {  

186.                fprintf(stderr, "Failed object:

 ");  

187.                for (count = 1, vars = response->variables;  

188.                     vars && count !

= response->errindex;  

189.                     vars = vars->next_variable, count++)  

190.                    /*EMPTY*/;  

191.                if (vars) {  

192.                    fprint_objid(stderr, vars->name, vars->name_length);  

193.                }  

194.                fprintf(stderr, "\n");  

195.            }  

196.            exitval = 2;  

197.              

198.              

199.            if (!

netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,  

200.                                        NETSNMP_DS_APP_DONT_FIX_PDUS)) {  

201.                pdu = snmp_fix_pdu(response, SNMP_MSG_GET);  

202.                snmp_free_pdu(response);  

203.                response = NULL;  

204.                if (pdu !

= NULL) {  

205.                    goto retry;  

206.                }  

207.            }  

208.        }                       /* endif -- SNMP_ERR_NOERROR */  

209.          

210.    } else if (status == STAT_TIMEOUT) {  

211.        fprintf(stderr, "Timeout:

 No Response from %s.\n",  

212.                session.peername);  

213.        exitval = 1;  

214.          

215.    } else {                    /* status == STAT_ERROR */  

216.        snmp_sess_perror("snmpget", ss);  

217.        exitval = 1;  

218.          

219.    }                           /* endif -- STAT_SUCCESS */      

220.      

221.    if (response)  

222.        snmp_free_pdu(response);  

223.    snmp_close(ss);  

224.      

225.    SOCK_CLEANUP;  

226.      

227.    return "";  

228.}  

229.  

230.   int main()  

231.{  

232.    string strSnmpInfo;  

233.    //    strSnmpInfo=SnmpGet("2c","public","192.168.1.241",".1.3.6.1.4.1.11.2.3.9.1.1.7.0");  

234.    //    strSnmpInfo=SnmpGet("2c","public","192.168.1.241",".1.3.6.1.2.1.1.1.0");  

235.    strSnmpInfo=SnmpGet("public","192.168.1.133","1.3.6.1.2.1.1.1.0");  

236.        

237.    cout<<"wyz---------------snmpInfo:

:

"<

238.    return 0;  

239.}  

#include

#ifHAVE_STDLIB_H

#include

#endif

#ifHAVE_UNISTD_H

#include

#endif

#ifHAVE_STRING_H

#include

#else

#include

#endif

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > PPT模板 > 其它模板

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1