欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

c#开发snmp应用 - 软件开发随记

更新时间: 2015-05-04 00:00:00 责任编辑: Author_N14

 

    首先准备一个包snmpsharpnet,到这个官网上去下载http://www.snmpsharpnet.com/

    我主要关心两个方式,一个是通过snmpget方法获得,一个是通过snmpwalk方法,snmpget方法可以获得指定oid的值,snmpwalk方法可以获得一个组下面的所有key和value。

    剩下的不多说,直接贴代码吧。

 

using System;
using System.Collections.Generic;
using System.Web;
using System.Net;
using SnmpSharpNet;


/// <summary>
///SimpleSnmp 的摘要说明
/// </summary>
public class SimpleSnmp
{
	public SimpleSnmp()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
    }

    #region 通过oid字符数组获得相应的值
    public static Dictionary<string,string> getOIDValue(string host, string[] oid) {
        //返回变量
        Dictionary<string, string> dic = new Dictionary<string, string>();

        // 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 = 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(host);

        // Construct target
        UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);

        // Pdu class used for all requests
        Pdu pdu = new Pdu(PduType.Get);

        foreach (string singleoid in oid) {
            pdu.VbList.Add(singleoid);
        }

        // 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)
            {
                for (int i = 0; i < result.Pdu.VbList.Count;i++ )
                {
                    dic.Add(result.Pdu.VbList[i].Oid.ToString(), result.Pdu.VbList[i].Value.ToString());
                }
                // Reply variables are returned in the same order as they were added
                //  to the VbList
            }
        }
        target.Close();
        return dic;
    }
    #endregion

    #region 通过snmpwalk返回oid根下面的所有值
    public static Dictionary<string, string> getWalkValue(string host,string irootOid) {
        Dictionary<string, string> dic = new Dictionary<string, string>();
        // 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 = 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(host);

        // 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(irootOid); // 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;
            }
            // 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)
                {
                    // 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))
                        {
                            dic.Add(v.Oid.ToString(), v.Value.ToString());
                        }
                        else
                        {
                            // we have reached the end of the requested
                            // MIB tree. Set lastOid to null and exit loop
                            lastOid = null;
                        }
                    }
                }
            }
        }
        target.Close();
        return dic;
    }
    #endregion
}

  代码基本上都是官网给出的,我觉得这两个对我来说比较有用,就整理了一下,具体的调用我想大家应该会

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2015-05-04 00:00:00
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/1724932/
本文WWW.DOC100.NET DOC100.NET版权所有。