码迷,mamicode.com
首页 > Windows程序 > 详细

Visual C# 2015调用SnmpSharpNet库实现简单的SNMP元素查询

时间:2017-09-10 13:38:32      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:pad   desktop   .net   png   ace   community   tag   parameter   调用   

一开始调研发现有几个SNMP的库,

一个是net-SNMP,这个好像是linux用的多

一个是微软自己的WinSNMP,这个没有例子,不太好操作

一个是SnmpSharpNet,这个有些例子比较好,

利用SnmpSharpNet的例子实现读取udpindatagrams的代码如下:

要将SnmpSharpNet.dll放入Visual C# 2015的工程目录下,然后,在工程浏览器的引用里添加这个dll,方法见这里,引用,右键,添加引用,浏览到dll即可

技术分享

 1 using System;
 2 using System.Net;
 3 using SnmpSharpNet;
 4 
 5 namespace snmpget
 6 {
 7     class Program
 8     {
 9         static void Main(string[] args)
10         {
11             // SNMP community name
12             OctetString community = new OctetString("public");
13 
14             // Define agent parameters class
15             AgentParameters param = new AgentParameters(community);
16             // Set SNMP version to 1 (or 2)
17             param.Version = SnmpVersion.Ver1;
18             // Construct the agent address object
19             // IpAddress class is easy to use here because
20             //  it will try to resolve constructor parameter if it doesn‘t
21             //  parse to an IP address
22             IpAddress agent = new IpAddress("192.168.0.10");
23 
24             // Construct target
25             UdpTarget target = new UdpTarget((IPAddress)agent, 161, 2000, 1);
26 
27             // Pdu class used for all requests
28             Pdu pdu = new Pdu(PduType.Get);
29             pdu.VbList.Add("1.3.6.1.2.1.7.1.0"); //udpindatagrams
30 
31             // Make SNMP request
32             SnmpV1Packet result = (SnmpV1Packet)target.Request(pdu, param);
33 
34             // If result is null then agent didn‘t reply or we couldn‘t parse the reply.
35             if (result != null)
36             {
37                 // ErrorStatus other then 0 is an error returned by 
38                 // the Agent - see SnmpConstants for error definitions
39                 if (result.Pdu.ErrorStatus != 0)
40                 {
41                     // agent reported an error with the request
42                     Console.WriteLine("Error in SNMP reply. Error {0} index {1}",
43                         result.Pdu.ErrorStatus,
44                         result.Pdu.ErrorIndex);
45                 }
46                 else
47                 {
48                     // Reply variables are returned in the same order as they were added
49                     //  to the VbList
50                     Console.WriteLine("sysDescr({0}) ({1}): {2}",
51                         result.Pdu.VbList[0].Oid.ToString(),
52                         SnmpConstants.GetTypeName(result.Pdu.VbList[0].Value.Type),
53                         result.Pdu.VbList[0].Value.ToString());
54                 }
55             }
56             else
57             {
58                 Console.WriteLine("No response received from SNMP agent.");
59             }
60             target.Close();
61         }
62     }
63 }

 输出结果如下:

技术分享

发送和接收的包的wireshark截图:

get-request

技术分享

get-response

技术分享

 

接下来,需要看如何获取网络拓扑。。。

Visual C# 2015调用SnmpSharpNet库实现简单的SNMP元素查询

标签:pad   desktop   .net   png   ace   community   tag   parameter   调用   

原文地址:http://www.cnblogs.com/yanhc/p/7500669.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!