当前位置:   article > 正文

linux net-snmp agent manager windows snmp_linux net-snmp client

linux net-snmp client

这段时间一直在弄snmp相关的东西,网上的资料有用的很少,在这说下自己遇到的问题避免很多人都走回弯路。

snmp分为manager管理端(一般为一台主机),和agent代理端(一般为一些运行SNMP的设备);对应到net-snmp为client 和 server端(agent对应server)。

windows下snmp测试验证最好使用MG-SOFT Mib Browser 9破解版,有可能会遇到兼容性问题,右键兼容性疑难解答就可以了,这个软件使用可以参考: http://pan.baidu.com/share/link?shareid=3554272301&uk=389306673&fid=4210277839

windows本机需要开启SNMP Service,在服务中属性





其中安全页h3c代表访问这台机器的密码,很多默认是public,如无加密需求改成public即可。 当你访问很多设备时,很有可能不是snmpget ... public,而是这台设备对应的加密字符。

windows下也可以使用snmputil.exe这个工具来验证。

重点说下linux下net-snmp,我用的是ubuntu12.04系统,这里安装的是net-snmp-5.5.1。

首先按这个网址安装http://blog.csdn.net/yinjiabin/article/details/7564241第一步;之后参考 http://wenku.baidu.com/link?url=Z94_Up5dMA9kaH85zDt1iyJ46A-_nlYxQCzRjeBkts3vRd_9yRdiOIeeT0LMRSgdxiR9zDyXAWLwRQGvo4t78IMqfeV6TJ1oRfo6536ijnK;其中第6步snmpd.conf参考 http://www.linuxidc.com/Linux/2012-12/76837.htm,其中忽略第5步。

如果是manager开发可以参考下这两个网址:http://net-snmp.sourceforge.net/tutorial/tutorial-5/toolkit/asyncapp/asyncapp.c  和 http://zhubangbing.blog.163.com/blog/static/526092702011728102831294/  都是可以编译的,  我参照这两个也封装了一下snmpget和snmpset函数可以参考下

  1. #include <net-snmp/net-snmp-config.h>
  2. #include <net-snmp/net-snmp-includes.h>
  3. #include <iostream>
  4. using namespace std;
  5. #ifdef HAVE_WINSOCK_H
  6. #include <winsock.h>
  7. #endif
  8. int netsnmp_get(char *vision, char *community, char *ip, char *oid_name, netsnmp_variable_list **vars)
  9. {
  10. SOCK_STARTUP;
  11. init_snmp("asynchapp");
  12. struct snmp_session ss, *sp;
  13. struct oid1 *op;
  14. snmp_sess_init(&ss);
  15. if("1" == vision)
  16. ss.version = SNMP_VERSION_1;
  17. else if("2c" == vision)
  18. ss.version = SNMP_VERSION_2c;
  19. else if("3" == vision)
  20. ss.version = SNMP_VERSION_3;
  21. ss.peername = strdup(ip);
  22. ss.community = (u_char *)(community);
  23. ss.community_len = strlen(community);
  24. if(!(sp = snmp_open(&ss)))
  25. {
  26. std::cout << "snmp_open ip : " << ip << " oid :" << oid_name << " failed\n";
  27. snmp_perror("snmp_open failed\n");
  28. SOCK_CLEANUP;
  29. return STAT_ERROR;
  30. }
  31. oid Oid[MAX_OID_LEN];
  32. int OidLen = MAX_OID_LEN;
  33. if(!read_objid(oid_name, Oid, (size_t *)&OidLen))
  34. {
  35. std::cout << "read_objid ip : " << ip << " oid :" << oid_name << " failed\n";;
  36. snmp_perror("read_objid failed\n");
  37. snmp_close(sp);
  38. SOCK_CLEANUP;
  39. return STAT_ERROR;
  40. }
  41. else
  42. {
  43. struct snmp_pdu *req, *resp;
  44. int status;
  45. req = snmp_pdu_create(SNMP_MSG_GET);
  46. snmp_add_null_var(req, Oid, (size_t)OidLen);
  47. status = snmp_synch_response(sp, req, &resp);
  48. *vars = resp->variables;
  49. snmp_free_pdu(resp);
  50. snmp_close(sp);
  51. switch (status) {
  52. case STAT_SUCCESS:
  53. if(resp->errstat == SNMP_ERR_NOERROR){}
  54. else
  55. {
  56. std::cout << "Error in packet\nReason: " << snmp_errstring(resp->errstat) << "\n";
  57. }
  58. break;
  59. case STAT_TIMEOUT:
  60. std::cout << "Timeout: No Response from " << sp->peername << "\n";
  61. break;
  62. case STAT_ERROR:
  63. std::cout << "snmpget Error from " << sp->peername << "\n";
  64. snmp_perror(sp->peername);
  65. break;
  66. }
  67. SOCK_CLEANUP;
  68. return status;
  69. }
  70. }
  71. int netsnmp_set(char *vision, char *community, char *ip, char *oid_name, char type, char *value)
  72. {
  73. SOCK_STARTUP;
  74. init_snmp("asynchapp");
  75. struct snmp_session ss, *sp;
  76. struct oid1 *op;
  77. snmp_sess_init(&ss);
  78. if("1" == vision)
  79. ss.version = SNMP_VERSION_1;
  80. else if("2c" == vision)
  81. ss.version = SNMP_VERSION_2c;
  82. else if("3" == vision)
  83. ss.version = SNMP_VERSION_3;
  84. ss.peername = strdup(ip);
  85. ss.community = (u_char *)(community);
  86. ss.community_len = strlen(community);
  87. if(!(sp = snmp_open(&ss)))
  88. {
  89. std::cout << "snmp_open ip : " << ip << " oid :" << oid_name << " failed\n";
  90. snmp_perror("snmp_open failed\n");
  91. SOCK_CLEANUP;
  92. return STAT_ERROR;
  93. }
  94. oid Oid[MAX_OID_LEN];
  95. int OidLen = MAX_OID_LEN;
  96. if(!read_objid(oid_name, Oid, (size_t *)&OidLen))
  97. {
  98. std::cout << "read_objid ip : " << ip << " oid :" << oid_name << " failed\n";;
  99. snmp_perror("read_objid failed\n");
  100. snmp_close(sp);
  101. SOCK_CLEANUP;
  102. return STAT_ERROR;
  103. }
  104. else
  105. {
  106. struct snmp_pdu *req, *resp;
  107. int status;
  108. req = snmp_pdu_create(SNMP_MSG_SET);
  109. snmp_add_var(req, Oid, (size_t)OidLen, type, value);
  110. status = snmp_synch_response(sp, req, &resp);
  111. snmp_free_pdu(resp);
  112. snmp_close(sp);
  113. switch (status) {
  114. case STAT_SUCCESS:
  115. if(resp->errstat == SNMP_ERR_NOERROR){}
  116. else
  117. {
  118. std::cout << "Error in packet\nReason: " << snmp_errstring(resp->errstat) << "\n";
  119. }
  120. break;
  121. case STAT_TIMEOUT:
  122. std::cout << "Timeout: No Response from " << sp->peername << "\n";
  123. break;
  124. case STAT_ERROR:
  125. std::cout << "snmpset Error from " << sp->peername << "\n";
  126. snmp_perror(sp->peername);
  127. break;
  128. }
  129. SOCK_CLEANUP;
  130. return status;
  131. }
  132. }
  133. int main(int argc, char **argv)
  134. {
  135. netsnmp_variable_list *vars;
  136. char buf[1024];
  137. if(netsnmp_get("2c", "public", "192.168.1.50", "1.3.6.1.4.1", &vars) == STAT_SUCCESS)
  138. {
  139. while(vars)
  140. {
  141. snprint_variable(buf, sizeof(buf), vars->name, vars->name_length, vars);
  142. std::cout << buf << std::endl;
  143. vars = vars->next_variable;
  144. }
  145. }
  146. netsnmp_set("2c", "public", "192.168.1.50", "1.3.6.1.4.1", 'i', "6");
  147. if(netsnmp_get("2c", "public", "192.168.1.50", "1.3.6.1.4.1", &vars) == STAT_SUCCESS)
  148. {
  149. while(vars)
  150. {
  151. snprint_variable(buf, sizeof(buf), vars->name, vars->name_length, vars);
  152. std::cout << buf << std::endl;
  153. vars = vars->next_variable;
  154. }
  155. }
  156. return 0;
  157. }
agent设备可以参考: http://blog.csdn.net/shanzhizi/article/details/11769491  http://blog.csdn.net/zhulinfeiba/article/details/4202935

遇到了问题可以到它的官网去搜索下关键字http://net-snmp.sourceforge.net/tutorial/tutorial-5/toolkit/ ,我是没测试成功...

我的只是manager部分,agent部分不涉及,祝你好运


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/156638
推荐阅读
相关标签
  

闽ICP备14008679号