Apparently not everybody is as interested in the background workings of the SNMP protocol as I am. for this reason I have created a very simple to use utility class that will allow you to make requests and collect replies without worrying too much about how it's all done.
For that reason I have created simplesnmp class. it makes common SNMP Version 1 and 2c operations simple. version 3 is not supported ded because there is a security aspect that, if simplified too much, will no longer be secure.
To dive straight in, here is a SNMP-GET request example:
String Host = "Localhost" ; String Community = "Public" ; Simplesnmp SNMP = New Simplesnmp ( Host, community) ; If ( ! SNMP. Valid ) { Console. Writeline ( "SNMP agent host name/IP address is invalid ." ) ; Return ; } Dictionary< OID, asntype > Result = SNMP. Get ( Snmpversion. Ver1 , New String [ ] { ". 1.3.6.1.2.1.1.1.0" } ) ; If ( Result = Null ) { Console. Writeline ( "No results received ." ) ; Return ; } Foreach ( Keyvaluepair kVpIn Result ) { Console. Writeline ( "{0 }:{ 1} {2 }" , KVp. Key . Tostring ( ) , Snmpconstants. Gettypename ( KVp. Value . Type ) , KVp. Value . Tostring ( ) ) ; }
On my laptop, result looks like this:
1.3.6.1.2.1.1.1.0: octetstring "dual core intel notebook"
Obviusly, you can request multiple values in a single request just by adding them to the simplesnmp. Get () oId string array.
Methods are also available for getnext:
Dictionary < OID, asntype > Result = SNMP. Getnext ( Snmpversion. Ver2 , New String [ ] { ". 1.3.6.1.2.1.1.1" , ". 1.3.6.1.2.1.1.2" } ) ;
GETBULK:
dictionary OID, asntype > result = SNMP. GETBULK ( New string [ ] { ". 1.3.6.1.2 ", ". 1.3.6.1.3 " } ) ;
Set:
Dictionary < OID, asntype > Result = SNMP. Set ( Snmpversion. Ver2 , New VB [ ] { New VB ( New Oid ( ". 1.3.6.1.2.1.1.1.0" ) , New Octetstring ( "New sysdescr.0" ) } ) ;
And walk:
Dictionary result=SNMP.Walk(Snmpversion.Ver2,". 1.3.6.1.2.1.1.1");
Simplesnmp. Walk uses GETBULK with SNMP Version 2C. If SNMP Version 1 is selected, getnext operation is used and is considerably slower.
To control how GETBULK and SNMP Version 2 walk performs, you can set simplesnmp. nonrepeaters and simplesnmp. MaxRepetitions values to adjust how getbulk cils are made.
Prior to using the class, you can check the status of the simplesnmp. valid property to verify class is in correctly initialized. this property will validate that agent name was correctly resolved to an IP address and that community name is set.
From: http://www.snmpsharpnet.com/node/19