赞
踩
这段时间一直在弄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函数可以参考下
- #include <net-snmp/net-snmp-config.h>
- #include <net-snmp/net-snmp-includes.h>
- #include <iostream>
-
- using namespace std;
-
- #ifdef HAVE_WINSOCK_H
- #include <winsock.h>
- #endif
-
-
- int netsnmp_get(char *vision, char *community, char *ip, char *oid_name, netsnmp_variable_list **vars)
- {
- SOCK_STARTUP;
- init_snmp("asynchapp");
-
- struct snmp_session ss, *sp;
- struct oid1 *op;
-
- snmp_sess_init(&ss);
- if("1" == vision)
- ss.version = SNMP_VERSION_1;
- else if("2c" == vision)
- ss.version = SNMP_VERSION_2c;
- else if("3" == vision)
- ss.version = SNMP_VERSION_3;
- ss.peername = strdup(ip);
- ss.community = (u_char *)(community);
- ss.community_len = strlen(community);
- if(!(sp = snmp_open(&ss)))
- {
- std::cout << "snmp_open ip : " << ip << " oid :" << oid_name << " failed\n";
- snmp_perror("snmp_open failed\n");
- SOCK_CLEANUP;
- return STAT_ERROR;
- }
-
- oid Oid[MAX_OID_LEN];
- int OidLen = MAX_OID_LEN;
- if(!read_objid(oid_name, Oid, (size_t *)&OidLen))
- {
- std::cout << "read_objid ip : " << ip << " oid :" << oid_name << " failed\n";;
- snmp_perror("read_objid failed\n");
- snmp_close(sp);
- SOCK_CLEANUP;
- return STAT_ERROR;
- }
- else
- {
- struct snmp_pdu *req, *resp;
- int status;
- req = snmp_pdu_create(SNMP_MSG_GET);
- snmp_add_null_var(req, Oid, (size_t)OidLen);
- status = snmp_synch_response(sp, req, &resp);
- *vars = resp->variables;
- snmp_free_pdu(resp);
- snmp_close(sp);
-
- switch (status) {
- case STAT_SUCCESS:
- if(resp->errstat == SNMP_ERR_NOERROR){}
- else
- {
- std::cout << "Error in packet\nReason: " << snmp_errstring(resp->errstat) << "\n";
- }
- break;
- case STAT_TIMEOUT:
- std::cout << "Timeout: No Response from " << sp->peername << "\n";
- break;
- case STAT_ERROR:
- std::cout << "snmpget Error from " << sp->peername << "\n";
- snmp_perror(sp->peername);
- break;
- }
- SOCK_CLEANUP;
- return status;
- }
- }
-
- int netsnmp_set(char *vision, char *community, char *ip, char *oid_name, char type, char *value)
- {
- SOCK_STARTUP;
- init_snmp("asynchapp");
-
- struct snmp_session ss, *sp;
- struct oid1 *op;
-
- snmp_sess_init(&ss);
- if("1" == vision)
- ss.version = SNMP_VERSION_1;
- else if("2c" == vision)
- ss.version = SNMP_VERSION_2c;
- else if("3" == vision)
- ss.version = SNMP_VERSION_3;
- ss.peername = strdup(ip);
- ss.community = (u_char *)(community);
- ss.community_len = strlen(community);
- if(!(sp = snmp_open(&ss)))
- {
- std::cout << "snmp_open ip : " << ip << " oid :" << oid_name << " failed\n";
- snmp_perror("snmp_open failed\n");
- SOCK_CLEANUP;
- return STAT_ERROR;
- }
-
- oid Oid[MAX_OID_LEN];
- int OidLen = MAX_OID_LEN;
- if(!read_objid(oid_name, Oid, (size_t *)&OidLen))
- {
- std::cout << "read_objid ip : " << ip << " oid :" << oid_name << " failed\n";;
- snmp_perror("read_objid failed\n");
- snmp_close(sp);
- SOCK_CLEANUP;
- return STAT_ERROR;
- }
- else
- {
- struct snmp_pdu *req, *resp;
- int status;
- req = snmp_pdu_create(SNMP_MSG_SET);
- snmp_add_var(req, Oid, (size_t)OidLen, type, value);
- status = snmp_synch_response(sp, req, &resp);
- snmp_free_pdu(resp);
- snmp_close(sp);
-
- switch (status) {
- case STAT_SUCCESS:
- if(resp->errstat == SNMP_ERR_NOERROR){}
- else
- {
- std::cout << "Error in packet\nReason: " << snmp_errstring(resp->errstat) << "\n";
- }
- break;
- case STAT_TIMEOUT:
- std::cout << "Timeout: No Response from " << sp->peername << "\n";
- break;
- case STAT_ERROR:
- std::cout << "snmpset Error from " << sp->peername << "\n";
- snmp_perror(sp->peername);
- break;
- }
- SOCK_CLEANUP;
- return status;
- }
- }
-
- int main(int argc, char **argv)
- {
- netsnmp_variable_list *vars;
- char buf[1024];
- if(netsnmp_get("2c", "public", "192.168.1.50", "1.3.6.1.4.1", &vars) == STAT_SUCCESS)
- {
- while(vars)
- {
- snprint_variable(buf, sizeof(buf), vars->name, vars->name_length, vars);
- std::cout << buf << std::endl;
- vars = vars->next_variable;
- }
- }
-
- netsnmp_set("2c", "public", "192.168.1.50", "1.3.6.1.4.1", 'i', "6");
-
- if(netsnmp_get("2c", "public", "192.168.1.50", "1.3.6.1.4.1", &vars) == STAT_SUCCESS)
- {
- while(vars)
- {
- snprint_variable(buf, sizeof(buf), vars->name, vars->name_length, vars);
- std::cout << buf << std::endl;
- vars = vars->next_variable;
- }
- }
-
-
- return 0;
- }
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部分不涉及,祝你好运
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。