您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 公司方案 > LINUX环境下用C实现SNMP网管协议
RFCRFC1155SMIStructureandIdentificationofManagementInformationForTCP/IP-basedInternetRFC1157(SNMPSimpleNetworkManagerProtocal)RFC1213MIBIIManagementInformationBaseCarnegieMellonUniversity()SNMPLINUXCSNMP1ManagerSNMPLINUXAgentSNMP2MCSNMPGETGET-NEXTSETSNMPSNMPMASNMPPDUMGETGET-NEXTAMSETAAM3SNMP(MIB)(MIB)5Internet(MIB),SNMPManagerSNMPAgentSendTrapPort162Port161Get-ResponseGet-RequestGet-Next-RequestSet-RequestMIB-IMIBSystemInterfaceATIPICMPTCPUDPEGPMIBDEFINITIONS::=BEGINIMPORTSmgmt,OBJECT-TYPE,NetworkAddress,IpAddress,Counter,Gauge,TimeTicksmibOBJECTIDENTIFIER::={mgmt1}systemOBJECTIDENTIFIER::={mgmt2}interfaceOBJECTIDENTIFIER::={mgmt3}atOBJECTIDENTIFIER::={mgmt4}ipOBJECTIDENTIFIER::={mgmt5}icmpOBJECTIDENTIFIER::={mgmt6}tcpOBJECTIDENTIFIER::={mgmt7}udpOBJECTIDENTIFIER::={mgmt8}egpOBJECTIDENTIFIER::={mgmt9}ASN.1(BER)1136142MIB-IROOTISOrganizationsSystemInterfaceATIPICMTCUDEGManagementDODInternetPrivate(mgmt)1(Integer)02(Old)06(BitString)03(sequence)308(OctetString)0430(null)052SNMPPDUProtocolDataUnitSNMPSNMPPDUtypeRequest-idSNMPPDUSNMPPDUtypeRequest-id00variable-bindingsGetRequestPDU,GetNextRequestPDU,SetRequestPDUPDUtypeRequest-iderror-statuserror-indexvariable-bindingsGetResponsePDUPDUtypeEnterpriseagent-addrGeneric-trapspcific-traptime-stampvariable-bindingsTrapPDUName1Value1name2value2…namenvaluenvariable-bindingsSNMPGetRequestPDU(PUD)GetRequest-PDU::=IMPLICITSEQUENCE{request-idRequestID,--always0error-statusErrorStatus,--always0error-indexErrorIndex,--always0variable-bindingsVarBindList}Request-ID48ErrorstatusErrorindex08VarBindlistASN.1VarBindlistSNMPv1ASN.1ASN.1GetRequest0xA0(160)GetNextRequset0xA1(161)GetRqsponse0xA2(162)SetRequest0xA3(163)Trap0xA4(164)SNMPSNMP2(ProtocolHeader)(Dataarea)SNMPcommunity()PDUPDUSNMPcommunityDataArea(PDU)SNMPSNMPASN.1SNMPDEFINITIONS::=BFGINMessage::=SEQUENCE{version--version-1forthisRFCINTEGER{version-1(0)},community--communitynameOCTETSTRING,data--e.g.,PDUsiftrivialANY--authenticationisbeginusedPDUs::=CHOICE{get-requestGetRequest-PDU,get-next-requestGetNextRequest-PDUget-responseGetResponse-PDU,set-request-PDU,SetRequest-PDU,trapTrap-PDU}ENDSNMPSNMPASN.1BERsysDescr,MIBTreeID=1.3.6.1.2.1.1.1SNMP163029020100sequencelenintegerlenvers04067075626C6963stringlenpublicA01C020405AE5602getrequestlenintegerlen--------requestid-----020100020100integerlenerror-statusintegerlenerror-index300E300C0608sequencelensequencelenobjectidlen2B0601020101010005001.3.6.1.2.1.1.1.0null045SNMPGet-RequestGNUC++REDHAT6.0SNMPSNMP-(v2.7)LINUXSNMPMIB/*SNMPGET-REQUESTC++snmpgetIPOID*/#includesys/types.h#includenetinet/in.hPDUSNMP#includestdio.h#includesnmp.h#includesnmp_impl.h#includeasn1.h#includesnmp_api.h#includesnmp_client.hexterninterrno;intsnmp_dump_packet=0;main(argc,argv)intargc;char*argv[];{structsnmp_sessionsession,*ss;structsnmp_pdu*pdu,*response;structvariable_list*vars;intarg;char*gateway=NULL;char*community=NULL;intcount,current_name=0;char*names[128];oidname[MAX_NAME_LEN];intname_length;intstatus;init_mib();/**usage:snmpgetgateway-namecommunity-name*//**/for(arg=1;argargc;arg++){if(argv[arg][0]=='-'){switch(argv[arg][1]){case'd':snmp_dump_packet++;break;default:printf(invalidoption:-%c\n,argv[arg][1]);break;}continue;}if(gateway==NULL){gateway=argv[arg];}elseif(community==NULL){community=argv[arg];}else{names[current_name++]=argv[arg];}}if(!(gateway&&community&¤t_name0)){printf(usage:snmpget[-d]gateway-namecommunity-nameobject-identifier[object-identifier...]\n);exit(1);}/**/bzero((char*)&session,sizeof(structsnmp_session));session.peername=gateway;session.community=(u_char*)community;session.community_len=strlen((char*)community);session.retries=SNMP_DEFAULT_RETRIES;session.timeout=SNMP_DEFAULT_TIMEOUT;session.authenticator=NULL;snmp_synch_setup(&session);/**/ss=snmp_open(&session);if(ss==NULL){printf(Couldn'topensnmp\n);exit(-1);}/*PDU*/pdu=snmp_pdu_create(GET_REQ_MSG);for(count=0;countcurrent_name;count++){name_length=MAX_NAME_LEN;if(!read_objid(names[count],name,&name_length)){printf(Invalidobjectidentifier:%s\n,names[count]);}snmp_add_null_var(pdu,name,name_length);}retry:/**/status=snmp_synch_response(ss,pdu,&response);if(status==STAT_SUCCESS){if(response-errstat==SNMP_ERR_NOERROR){for(vars=response-variables;vars;vars=vars-next_variable)print_variable(vars-name,vars-name_length,vars);}else{printf(Errorinpacket.\nReason:%s\n,snmp_errstring(response-errstat));if(response-errstat==SNMP_ERR_NOSUCHNAME){printf(Thisnamedoesn'texist:);for(count=1,vars=response-variables;vars&&count!=response-errindex;vars=vars-next_variable,count++);if(vars)print_objid(vars-name,vars-name_length);printf(\n);}if((pdu=snmp_fix_pdu(response,GET_REQ_MSG))!=NULL)gotoretry;}}elseif(status==STAT_TIMEOUT){printf(NoResponsefrom%s\n,gateway);}else{/*status==STAT_ERROR*/printf(Anerroroccurred,Quitting\n);}/*PDU*/if(response)snmp_free_pdu(response);snmp_close(ss);}
本文标题:LINUX环境下用C实现SNMP网管协议
链接地址:https://www.777doc.com/doc-904777 .html