资源描述:
《c#下使用simplesnmp类》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、SimpleSNMPwithSimpleSnmpSat,11/21/2009-21:29—siteAdminApparentlynoteverybodyisasinterestedinthebackgroundworkingsoftheSNMPprotocolasIam.ForthisreasonIhavecreatedaverysimpletouseutilityclassthatwillallowyoutomakerequestsandcollectreplieswithoutworryingtoomuchabouthowit'salldone.ForthatreasonIhavecre
2、atedSimpleSnmpclass.ItmakescommonSNMPversion1and2coperationssimple.Version3isnotincludedbecausethereisasecurityaspectthat,ifsimplifiedtoomuch,willnolongerbesecure.Todivestraightin,hereisaSNMP-Getrequestexample:stringhost="localhost";stringcommunity="public";SimpleSnmpsnmp=newSimpleSnmp(host,communi
3、ty); if(!snmp.Valid){Console.WriteLine("SNMPagenthostname/ipaddressisinvalid.");return;}Dictionaryresult=snmp.Get(SnmpVersion.Ver1,newstring[]{".1.3.6.1.2.1.1.1.0"});if(result==null){Console.WriteLine("Noresultsreceived.");return;} foreach(KeyValuePairkvpinresult){Console.
4、WriteLine("{0}:{1}{2}",kvp.Key.ToString(),SnmpConstants.GetTypeName(kvp.Value.Type),kvp.Value.ToString());}Onmylaptop,resultlookslikethis:1.3.6.1.2.1.1.1.0:OctetString"DualcoreIntelnotebook"Obviously,youcanrequestmultiplevaluesinasinglerequestjustbyaddingthemtotheSimpleSnmp.Get()OIDstringarray.Meth
5、odsarealsoavailableforGetNext:Dictionaryresult=snmp.GetNext(SnmpVersion.Ver2,newstring[]{".1.3.6.1.2.1.1.1",".1.3.6.1.2.1.1.2"});GetBulk:Dictionaryresult=snmp.GetBulk(newstring[]{".1.3.6.1.2",".1.3.6.1.3"});Set:Dictionaryresult=snmp.Set(SnmpVersion.Ver2,newVb[
6、]{newVb(newOid(".1.3.6.1.2.1.1.1.0"),newOctetString("NewsysDescr.0")});andWalk:Dictionaryresult=snmp.Walk(SnmpVersion.Ver2,".1.3.6.1.2.1.1.1");SimpleSnmp.WalkusesGetBulkwithSNMPversion2c.IfSNMPversion1isselected,GetNextoperationisusedandisconsiderablyslower.TocontrolhowGetBulkandSNMPversion2walkper
7、forms,youcansetSimpleSnmp.NonRepeatersandSimpleSnmp.MaxRepetitionsvaluestoadjusthowGetBulkcallsaremade.Priortousingtheclass,youcancheckthestatusoftheSimpleSnmp.Validpropertytoverifyclassisincorrectlyinitial