最近需要在c#下面开发snmp的应用,其实我的需求很简单,就是通过一个oid可以获得一个值。在网上搜索了 一些,发现很多文章都是 你抄袭我我抄袭你,基本上拷贝下来是不能运行的,还有的一些利用系统库,有一些用付费的库。
在网上搜索了一下,发现有一个库相当的好用,SnmpSharpNet。官方网站为http://www.snmpsharpnet.com/。如果有需要请到官方网站上去下载开源的二进制文件,其中各个的介绍也相当的全面。在进行开发之前请先导入引用。
在snmp的语句中有两种语句,snmpget/snmpwalk我觉得这两个是我用的最多的,snmpget就是通过oid进行查找,而snmpwalk可以返回一个组中的数据。下面两段程序演示了具体怎么使用。
snmpget:源地址:http://www.snmpsharpnet.com/getexample.html
using System;
using System.Net;
using SnmpSharpNet;
namespace snmpget
{
class Program
{
static void Main(string[] args)
{
OctetString community = new OctetString("public");
AgentParameters param = new AgentParameters(community);
param.Version = (int)SnmpVersion.Ver1;
IpAddress agent = new IpAddress("127.0.0.1");
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.1.1.0");
pdu.VbList.Add("1.3.6.1.2.1.1.2.0");
pdu.VbList.Add("1.3.6.1.2.1.1.3.0");
pdu.VbList.Add("1.3.6.1.2.1.1.4.0");
pdu.VbList.Add("1.3.6.1.2.1.1.5.0");
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
if (result != null)
{
if (result.Pdu.ErrorStatus != 0)
{
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
Console.WriteLine("sysDescr({0}) ({1}): {2}",
result.Pdu.VbList[0].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
result.Pdu.VbList[0].Value.ToString());
Console.WriteLine("sysObjectID({0}) ({1}): {2}",
result.Pdu.VbList[1].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[1].Value.Type),
result.Pdu.VbList[1].Value.ToString());
Console.WriteLine("sysUpTime({0}) ({1}): {2}",
result.Pdu.VbList[2].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[2].Value.Type),
result.Pdu.VbList[2].Value.ToString());
Console.WriteLine("sysContact({0}) ({1}): {2}",
result.Pdu.VbList[3].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[3].Value.Type),
result.Pdu.VbList[3].Value.ToString());
Console.WriteLine("sysName({0}) ({1}): {2}",
result.Pdu.VbList[4].Oid.ToString(), SnmpConstants.GetTypeName(result.Pdu.VbList[4].Value.Type),
result.Pdu.VbList[4].Value.ToString());
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
target.Dispose();
}
}
snmpwalk:源代码地址:http://www.snmpsharpnet.com/walkexample.html
walk有两种方法实现,具体看源出处
using System;
using System.Net;
using SnmpSharpNet;
namespace sharpwalk
{
class Program
{
static void Main(string[] args)
{
OctetString community = new OctetString("public");
AgentParameters param = new AgentParameters(community);
param.Version = (int)SnmpVersion.Ver2;
IpAddress agent = new IpAddress("127.0.0.1");
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2");
Oid lastOid = (Oid)rootOid.Clone();
Pdu pdu = new Pdu(PduType.GetBulk);
pdu.NonRepeaters = 0;
pdu.MaxRepetitions = 5;
while (lastOid != null)
{
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
pdu.VbList.Clear();
pdu.VbList.Add(lastOid);
SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
if (result != null)
{
if (result.Pdu.ErrorStatus != 0)
{
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
lastOid = null;
break;
}
else
{
foreach (Vb v in result.Pdu.VbList)
{
if (rootOid.IsRootOf(v.Oid))
{
Console.WriteLine("{0} ({1}): {2}",
v.Oid.ToString(),
SnmpConstants.GetTypeName(v.Value.Type),
v.Value.ToString());
lastOid = v.Oid;
}
else
{
lastOid = null;
}
}
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
}
target.Dispose();
}
}
}
}