1、简单网络协议SNMP之net简单网络协议SNMP之net-snmp开发snmpget 分类: Ivan_OpenSource 2012-02-17 17:04 1437人阅读 评论(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
2、 ./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) make3) make install3.安装完毕在/usr/local/net-snmp底下内容如下:rootan-virtual-machine:/usr/local/net-snmp# lsbin include lib sbin sha
3、re进入lib,可以看到内容如下:rootan-virtual-machine:/usr/local/net-snmp/lib# lslibnetsnmp.a libnetsnmpmibs.lalibnetsnmpagent.a libnetsnmpmibs.solibnetsnmpagent.la libnetsnmpmibs.so.25libnetsnmpagent.so libnetsnmpmibs.so.25.0.2libnetsnmpagent.so.25 libnetsnmp.solibnetsnmpagent.so.25.0.2 libnetsnmp.so.25libnetsnm
4、phelpers.a libnetsnmp.so.25.0.2libnetsnmphelpers.la libnetsnmptrapd.alibnetsnmphelpers.so libnetsnmptrapd.lalibnetsnmphelpers.so.25 libnetsnmptrapd.solibnetsnmphelpers.so.25.0.2 libnetsnmptrapd.so.25libnetsnmp.la libnetsnmptrapd.so.25.0.2libnetsnmpmibs.a4.在Qtcreator,新建一个项目TestNetSNMp把上面lib里面的libnets
5、nmp.a放到项目底下在.pro里面添加内容如下:INCLUDEPATH +=/usr/local/net-snmp/includeLIBS += -L/root/wyz/TestNetSNMP -lnetsnmp5.在main里面添加代码如下:cpp view plaincopyprint?1. #include 2. 3. #ifHAVE_STDLIB_H 4. #include 5. #endif 6. #ifHAVE_UNISTD_H 7. #include 8. #endif 9. #ifHAVE_STRING_H 10. #include 11. #else 12. #includ
6、e 13. #endif 14. #include 15. #ifHAVE_NETINET_IN_H 16. #include 17. #endif 18. #include 19. #include 20. #ifTIME_WITH_SYS_TIME 21. #ifdefWIN32 22. #include 23. #else 24. #include 25. #endif 26. #include 27. #else 28. #ifHAVE_SYS_TIME_H 29. #include 30. #else 31. #include 32. #endif 33. #endif 34. #i
7、fHAVE_SYS_SELECT_H 35. #include 36. #endif 37. #ifHAVE_WINSOCK_H 38. #include 39. #endif 40. #ifHAVE_NETDB_H 41. #include 42. #endif 43. #ifHAVE_ARPA_INET_H 44. #include 45. #endif 46. 47. #include 48. 49. #include 50. #include 51. #include 52. #include 53. usingnamespacestd;54. 55. #defineNETSNMP_D
8、S_APP_DONT_FIX_PDUS0 56. 57. 58. 59. stringfprint_variable_1(constoid*objid,size_tobjidlen,constnetsnmp_variable_list*variable)60. 61. u_char*buf=NULL;62. size_tbuf_len=256,out_len=0;63. if(buf=(u_char*)calloc(buf_len,1)=NULL)64. 65. return;66. else67. if(sprint_realloc_variable(&buf,&buf_len,&out_l
9、en,1,68. objid,objidlen,variable)69. stringstrTemp(char*)buf);70. 71. intiFirst=strTemp.find_first_of(:);72. stringstrInfo=strTemp.substr(iFirst,strTemp.length()-1);73. 74. returnstrInfo;75. 76. else77. fprintf(stdout,%sTRUNCATEDn,buf);78. 79. 80. SNMP_FREE(buf);81. 82. 83. stringfprint_variable_2(c
10、onstoid*objid,size_tobjidlen,constnetsnmp_variable_list*variable)84. 85. u_char*buf=NULL;86. size_tbuf_len=256,out_len=0;87. if(buf=(u_char*)calloc(buf_len,1)=NULL)88. 89. return;90. else91. if(sprint_realloc_variable(&buf,&buf_len,&out_len,1,92. objid,objidlen,variable)93. stringstrTemp(char*)buf);
11、94. 95. returnstrTemp;96. 97. else98. fprintf(stdout,%sTRUNCATEDn,buf);99. 100. 101. SNMP_FREE(buf);102. 103. 104. stringSnmpGet(char*community,char*ip,char*oid_array)/snmpget-v2c-cpublic10.0.0.111.3.6.1.2.1.1.5.0 105. 106. netsnmp_sessionsession,*ss=(netsnmp_session*)malloc(sizeof(netsnmp_session);
12、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. intcount
13、;119. intcurrent_name=0;120. char*namesSNMP_MAX_CMDLINE_OIDS;121. oidnameMAX_OID_LEN;122. size_tname_length;123. intstatus;124. intfailures=0;125. intexitval=0;126. 127. snmp_sess_init(&session);128. 129. session.version=SNMP_VERSION_2c;130. 131. session.peername=ip;132. munity=(unsignedchar*)commun
14、ity;133. munity_len=strlen(community);134. 135. 136. names0=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
15、_GET);151. for(count=0;counterrstat=SNMP_ERR_NOERROR)172. 173. vars=response-variables;174. 175. stringstrTemp=fprint_variable_1(vars-name,vars-name_length,vars);176. 177. returnstrTemp;178. 179. else180. 181. printf(2n);182. fprintf(stderr,ErrorinpacketnReason:%sn,183. snmp_errstring(response-errst
16、at);184. 185. if(response-errindex!=0)186. fprintf(stderr,Failedobject:);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. 1
17、96. 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. gotoretry;206. 207. 208. /*endif-SNMP_ERR_NOERROR*/209. 210. elseif(
18、status=STAT_TIMEOUT)211. fprintf(stderr,Timeout:NoResponsefrom%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
19、. SOCK_CLEANUP;226. 227. return;228. 229. 230. intmain()231. 232. stringstrSnmpInfo;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. coutwyz-snmpInfo:strSnmpInfoendl;238. return0;239. #include #if HAVE_STDLIB_H#include #endif#if HAVE_UNISTD_H#include #endif#if HAVE_STRING_H#include #else#include #endif
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1