Simple SNMP with SimpleSnmp

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 included 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 kvp in 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"

Obviously, 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 calls 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.

 

 摘自:http://www.snmpsharpnet.com/node/19

转载于:https://www.cnblogs.com/EasyData/archive/2010/01/23/1654929.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值