C#下利用SnmpSharpNet进行snmp开发
最近需要在c#下面开发snmp的应用,其实我的需求很简单,就是通过一个oid可以获得一个值。在网上搜索了 一些,发现很多文章都是 你抄袭我我抄袭你,基本上拷贝下来是不能运行的,还有的一些利用系统库,有一些用付费的库。
在网上搜索了一下,发现有一个库相当的好用,SnmpSharpNet。为www.snmpsharpnet/。如果有需要请到上去下载开源的二进制文件,其中各个的介绍也相当的全面。在进行开发之前请先导入引用。
在snmp的语句中有两种语句,snmpget/snmpwalk我觉得这两个是我用的最多的,snmpget就是通过oid进行查,而snmpwalk可以返回一个组中的数据。下面两段程序演示了具体怎么使用。
snmpget:源地址:www.snmpsharpnet/getexample.html
在网上搜索了一下,发现有一个库相当的好用,SnmpSharpNet。为www.snmpsharpnet/。如果有需要请到上去下载开源的二进制文件,其中各个的介绍也相当的全面。在进行开发之前请先导入引用。
在snmp的语句中有两种语句,snmpget/snmpwalk我觉得这两个是我用的最多的,snmpget就是通过oid进行查,而snmpwalk可以返回一个组中的数据。下面两段程序演示了具体怎么使用。
snmpget:源地址:www.snmpsharpnet/getexample.html
using System;
using System.Net;
using SnmpSharpNet;
using System.Net;
using SnmpSharpNet;
namespace snmpget
{
class Program
{
static void Main(string[] args)
{
// SNMP community name
OctetString community = new OctetString("public");
// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 1 (or 2)
param.Version = (int)SnmpVersion.Ver1;
// Construct the agent address object
// IpAddress class is easy to use here because
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
IpAddress agent = new IpAddress("127.0.0.1");
// Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr
pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
IpAddress agent = new IpAddress("127.0.0.1");
// Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.Get);
pdu.VbList.Add("1.3.6.1.2.1.1.1.0"); //sysDescr
pdu.VbList.Add("1.3.6.1.2.1.1.2.0"); //sysObjectID
pdu.VbList.Add("1.3.6.1.2.1.1.3.0"); //sysUpTime
pdu.VbList.Add("1.3.6.1.2.1.1.4.0"); //sysContact
pdu.VbList.Add("1.3.6.1.2.1.1.5.0"); //sysName
// Make SNMP request
SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
}
else
{
// Reply variables are returned in the same order as they were added
// to the VbList
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.
else
{
// Reply variables are returned in the same order as they were added
// to the VbList
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.");
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:源代码地址:www.snmpsharpnet/walkexample.html
walk有两种方法实现,具体看源出处
using System;
using System.Net;
using SnmpSharpNet;
namespace sharpwalk
{
class Program
{
static void Main(string[] args)
target.Dispose();
}
}
snmpwalk:源代码地址:www.snmpsharpnet/walkexample.html
walk有两种方法实现,具体看源出处
using System;
using System.Net;
using SnmpSharpNet;
namespace sharpwalk
{
class Program
{
static void Main(string[] args)
{
// SNMP community name
OctetString community = new OctetString("public");
// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)
param.Version = (int)SnmpVersion.Ver2;
// Construct the agent address object
// IpAddress class is easy to use here because
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
IpAddress agent = new IpAddress("127.0.0.1");
// Construct target
// SNMP community name
OctetString community = new OctetString("public");
// Define agent parameters class
AgentParameters param = new AgentParameters(community);
// Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)
param.Version = (int)SnmpVersion.Ver2;
// Construct the agent address object
// IpAddress class is easy to use here because
// it will try to resolve constructor parameter if it doesn't
// parse to an IP address
IpAddress agent = new IpAddress("127.0.0.1");
// Construct target
UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
// Define Oid that is the root of the MIB
// tree you wish to retrieve
Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr
// This Oid represents last Oid returned by
// the SNMP agent
Oid lastOid = (Oid)rootOid.Clone();
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.GetBulk);
// In this example, set NonRepeaters value to 0
pdu.NonRepeaters = 0;
// Define Oid that is the root of the MIB
// tree you wish to retrieve
Oid rootOid = new Oid("1.3.6.1.2.1.2.2.1.2"); // ifDescr
// This Oid represents last Oid returned by
// the SNMP agent
Oid lastOid = (Oid)rootOid.Clone();
// Pdu class used for all requests
Pdu pdu = new Pdu(PduType.GetBulk);
// In this example, set NonRepeaters value to 0
pdu.NonRepeaters = 0;
// MaxRepetitions tells the agent how many Oid/Value pairs to return
// in the response.
pdu.MaxRepetitions = 5;
// Loop through results
while (lastOid != null)
{
// When Pdu class is first constructed, RequestId is set to 0
// and during encoding id will be set to the random value
// for subsequent requests, id will be set to a value that
// needs to be incremented to have unique request ids for each
// packet
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
// in the response.
pdu.MaxRepetitions = 5;
// Loop through results
while (lastOid != null)
{
// When Pdu class is first constructed, RequestId is set to 0
// and during encoding id will be set to the random value
// for subsequent requests, id will be set to a value that
// needs to be incremented to have unique request ids for each
// packet
if (pdu.RequestId != 0)
{
pdu.RequestId += 1;
}
// Clear Oids from the Pdu class.
pdu.VbList.Clear();
// Initialize request PDU with the last retrieved Oid
pdu.VbList.Add(lastOid);
// Make SNMP request
SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
// You should catch exceptions in the Request if using in real application.
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
// Clear Oids from the Pdu class.
pdu.VbList.Clear();
// Initialize request PDU with the last retrieved Oid
pdu.VbList.Add(lastOid);
// Make SNMP request
SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);
// You should catch exceptions in the Request if using in real application.
// If result is null then agent didn't reply or we couldn't parse the reply.
if (result != null)
{
// ErrorStatus other then 0 is an error returned by
// the Agent - see SnmpConstants for error definitions
if (result.Pdu.ErrorStatus != 0)
{
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
lastOid = null;
break;
error parse new }
else
{
// Walk through returned variable bindings
foreach (Vb v in result.Pdu.VbList)
{
// Check that retrieved Oid is "child" of the root OID
if (rootOid.IsRootOf(v.Oid))
// agent reported an error with the request
Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
result.Pdu.ErrorStatus,
result.Pdu.ErrorIndex);
lastOid = null;
break;
error parse new }
else
{
// Walk through returned variable bindings
foreach (Vb v in result.Pdu.VbList)
{
// Check that retrieved Oid is "child" of the root OID
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
{
// we have reached the end of the requested
// MIB tree. Set lastOid to null and exit loop
lastOid = null;
}
}
}
Console.WriteLine("{0} ({1}): {2}",
v.Oid.ToString(),
SnmpConstants.GetTypeName(v.Value.Type),
v.Value.ToString());
lastOid = v.Oid;
}
else
{
// we have reached the end of the requested
// MIB tree. Set lastOid to null and exit loop
lastOid = null;
}
}
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
}
target.Dispose();
}
}
}
}
else
{
Console.WriteLine("No response received from SNMP agent.");
}
}
target.Dispose();
}
}
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论