您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > 简单网络协议SNMP之net
简单网络协议SNMP之net-snmp开发snmpget分类:Ivan@_OpenSource2012-02-1717:041437人阅读评论(0)收藏举报网络协议sessionincludestringnullcompiler目的:通过net-snmp关于snmpget源码的开发,从snmp代理设备获得自己想要的信息准备:从网址下载net-snmp-5.6.1.1.tar.gz源码包开发过程:1.解压源码2.进入解压源码的目录1)Run./configure--prefix=/usr/local/net-snmp(type./configure--helpforaquickusagesummary.)(--prefix=PATHwillchangethedefault/usr/localinstallationpath.)(seeCompilersandOptionsonchangingthecompilertouse)2)make3)makeinstall3.安装完毕在/usr/local/net-snmp底下内容如下:root@an-virtual-machine:/usr/local/net-snmp#lsbinincludelibsbinshare进入lib,可以看到内容如下:root@an-virtual-machine:/usr/local/net-snmp/lib#lslibnetsnmp.alibnetsnmpmibs.lalibnetsnmpagent.alibnetsnmpmibs.solibnetsnmpagent.lalibnetsnmpmibs.so.25libnetsnmpagent.solibnetsnmpmibs.so.25.0.2libnetsnmpagent.so.25libnetsnmp.solibnetsnmpagent.so.25.0.2libnetsnmp.so.25libnetsnmphelpers.alibnetsnmp.so.25.0.2libnetsnmphelpers.lalibnetsnmptrapd.alibnetsnmphelpers.solibnetsnmptrapd.lalibnetsnmphelpers.so.25libnetsnmptrapd.solibnetsnmphelpers.so.25.0.2libnetsnmptrapd.so.25libnetsnmp.lalibnetsnmptrapd.so.25.0.2libnetsnmpmibs.a4.在Qtcreator,新建一个项目TestNetSNMp把上面lib里面的libnetsnmp.a放到项目底下在.pro里面添加内容如下:INCLUDEPATH+=/usr/local/net-snmp/includeLIBS+=-L/root/wyz/TestNetSNMP-lnetsnmp5.在main里面添加代码如下:[cpp]viewplaincopyprint?1.#includenet-snmp/net-snmp-config.h2.3.#ifHAVE_STDLIB_H4.#includestdlib.h5.#endif6.#ifHAVE_UNISTD_H7.#includeunistd.h8.#endif9.#ifHAVE_STRING_H10.#includestring.h11.#else12.#includestrings.h13.#endif14.#includesys/types.h15.#ifHAVE_NETINET_IN_H16.#includenetinet/in.h17.#endif18.#includestdio.h19.#includectype.h20.#ifTIME_WITH_SYS_TIME21.#ifdefWIN3222.#includesys/timeb.h23.#else24.#includesys/time.h25.#endif26.#includetime.h27.#else28.#ifHAVE_SYS_TIME_H29.#includesys/time.h30.#else31.#includetime.h32.#endif33.#endif34.#ifHAVE_SYS_SELECT_H35.#includesys/select.h36.#endif37.#ifHAVE_WINSOCK_H38.#includewinsock.h39.#endif40.#ifHAVE_NETDB_H41.#includenetdb.h42.#endif43.#ifHAVE_ARPA_INET_H44.#includearpa/inet.h45.#endif46.47.#includenet-snmp/utilities.h48.49.#includenet-snmp/net-snmp-includes.h50.#includevector51.#includemap52.#includeiostream53.usingnamespacestd;54.55.#defineNETSNMP_DS_APP_DONT_FIX_PDUS056.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.}else{67.if(sprint_realloc_variable(&buf,&buf_len,&out_len,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.}else{77.fprintf(stdout,%s[TRUNCATED]\n,buf);78.}79.}80.SNMP_FREE(buf);81.}82.83.stringfprint_variable_2(constoid*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.}else{91.if(sprint_realloc_variable(&buf,&buf_len,&out_len,1,92.objid,objidlen,variable)){93.stringstrTemp((char*)buf);94.95.returnstrTemp;96.97.}else{98.fprintf(stdout,%s[TRUNCATED]\n,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.0105.{106.netsnmp_sessionsession,*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.intcount;119.intcurrent_name=0;120.char*names[SNMP_MAX_CMDLINE_OIDS];121.oidname[MAX_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.session.community=(unsignedchar*)community;133.session.community_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;countcurrent_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.else158.{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.stringstrTemp=fprint_variable_1(vars-name,vars-name_length,vars);176.177.returnstrTemp;178.}179.else180.{181.printf(2\n);182.fprintf(stderr,Errorinpacket\nReason:%s\n,183.snmp_errstring(response-e
本文标题:简单网络协议SNMP之net
链接地址:https://www.777doc.com/doc-2173774 .html