博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c#开发snmp应用

Posted on 2010-04-16 13:29  linFen  阅读(14524)  评论(3编辑  收藏  举报
 首先准备一个包snmpsharpnet,到这个官网上去下载http://www.snmpsharpnet.com/

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

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

 

C#代码 复制代码
  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.Web;   
  4. using System.Net;   
  5. using SnmpSharpNet;   
  6.   
  7.   
  8. /// <summary>   
  9. ///SimpleSnmp 的摘要说明   
  10. /// </summary>   
  11. public class SimpleSnmp   
  12. {   
  13.     public SimpleSnmp()   
  14.     {   
  15.         //   
  16.         //TODO: 在此处添加构造函数逻辑   
  17.         //   
  18.     }  
  19.  
  20.     #region 通过oid字符数组获得相应的值   
  21.     public static Dictionary<string,string> getOIDValue(string host, string[] oid) {   
  22.         //返回变量   
  23.         Dictionary<stringstring> dic = new Dictionary<stringstring>();   
  24.   
  25.         // SNMP community name   
  26.         OctetString community = new OctetString("public");   
  27.   
  28.         // Define agent parameters class   
  29.         AgentParameters param = new AgentParameters(community);   
  30.         // Set SNMP version to 1 (or 2)   
  31.         param.Version = SnmpVersion.Ver1;   
  32.         // Construct the agent address object   
  33.         // IpAddress class is easy to use here because   
  34.         //  it will try to resolve constructor parameter if it doesn't   
  35.         //  parse to an IP address   
  36.         IpAddress agent = new IpAddress(host);   
  37.   
  38.         // Construct target   
  39.         UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);   
  40.   
  41.         // Pdu class used for all requests   
  42.         Pdu pdu = new Pdu(PduType.Get);   
  43.   
  44.         foreach (string singleoid in oid) {   
  45.             pdu.VbList.Add(singleoid);   
  46.         }   
  47.   
  48.         // Make SNMP request   
  49.         SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);   
  50.   
  51.         // If result is null then agent didn't reply or we couldn't parse the reply.   
  52.         if (result != null)   
  53.         {   
  54.             // ErrorStatus other then 0 is an error returned by    
  55.             // the Agent - see SnmpConstants for error definitions   
  56.             if (result.Pdu.ErrorStatus == 0)   
  57.             {   
  58.                 for (int i = 0; i < result.Pdu.VbList.Count;i++ )   
  59.                 {   
  60.                     dic.Add(result.Pdu.VbList[i].Oid.ToString(), result.Pdu.VbList[i].Value.ToString());   
  61.                 }   
  62.                 // Reply variables are returned in the same order as they were added   
  63.                 //  to the VbList   
  64.             }   
  65.         }   
  66.         target.Close();   
  67.         return dic;   
  68.     }  
  69.     #endregion  
  70.  
  71.     #region 通过snmpwalk返回oid根下面的所有值   
  72.     public static Dictionary<stringstring> getWalkValue(string host,string irootOid) {   
  73.         Dictionary<stringstring> dic = new Dictionary<stringstring>();   
  74.         // SNMP community name   
  75.         OctetString community = new OctetString("public");   
  76.   
  77.         // Define agent parameters class   
  78.         AgentParameters param = new AgentParameters(community);   
  79.         // Set SNMP version to 2 (GET-BULK only works with SNMP ver 2 and 3)   
  80.         param.Version = SnmpVersion.Ver2;   
  81.         // Construct the agent address object   
  82.         // IpAddress class is easy to use here because   
  83.         //  it will try to resolve constructor parameter if it doesn't   
  84.         //  parse to an IP address   
  85.         IpAddress agent = new IpAddress(host);   
  86.   
  87.         // Construct target   
  88.         UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);   
  89.   
  90.         // Define Oid that is the root of the MIB   
  91.         //  tree you wish to retrieve   
  92.         Oid rootOid = new Oid(irootOid); // ifDescr   
  93.   
  94.         // This Oid represents last Oid returned by   
  95.         //  the SNMP agent   
  96.         Oid lastOid = (Oid)rootOid.Clone();   
  97.   
  98.         // Pdu class used for all requests   
  99.         Pdu pdu = new Pdu(PduType.GetBulk);   
  100.   
  101.         // In this example, set NonRepeaters value to 0   
  102.         pdu.NonRepeaters = 0;   
  103.         // MaxRepetitions tells the agent how many Oid/Value pairs to return   
  104.         // in the response.   
  105.         pdu.MaxRepetitions = 5;   
  106.   
  107.         // Loop through results   
  108.         while (lastOid != null)   
  109.         {   
  110.             // When Pdu class is first constructed, RequestId is set to 0   
  111.             // and during encoding id will be set to the random value   
  112.             // for subsequent requests, id will be set to a value that   
  113.             // needs to be incremented to have unique request ids for each   
  114.             // packet   
  115.             if (pdu.RequestId != 0)   
  116.             {   
  117.                 pdu.RequestId += 1;   
  118.             }   
  119.             // Clear Oids from the Pdu class.   
  120.             pdu.VbList.Clear();   
  121.             // Initialize request PDU with the last retrieved Oid   
  122.             pdu.VbList.Add(lastOid);   
  123.             // Make SNMP request   
  124.             SnmpV2Packet result = (SnmpV2Packet)target.Request(pdu, param);   
  125.             // You should catch exceptions in the Request if using in real application.   
  126.   
  127.             // If result is null then agent didn't reply or we couldn't parse the reply.   
  128.             if (result != null)   
  129.             {   
  130.                 // ErrorStatus other then 0 is an error returned by    
  131.                 // the Agent - see SnmpConstants for error definitions   
  132.                 if (result.Pdu.ErrorStatus == 0)   
  133.                 {   
  134.                     // Walk through returned variable bindings   
  135.                     foreach (Vb v in result.Pdu.VbList)   
  136.                     {   
  137.                         // Check that retrieved Oid is "child" of the root OID   
  138.                         if (rootOid.IsRootOf(v.Oid))   
  139.                         {   
  140.                             dic.Add(v.Oid.ToString(), v.Value.ToString());   
  141.                         }   
  142.                         else  
  143.                         {   
  144.                             // we have reached the end of the requested   
  145.                             // MIB tree. Set lastOid to null and exit loop   
  146.                             lastOid = null;   
  147.                         }   
  148.                     }   
  149.                 }   
  150.             }   
  151.         }   
  152.         target.Close();   
  153.         return dic;   
  154.     }  
  155.     #endregion   
  156. }