C# SNMP 编程
最近需要在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) { // 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 // 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.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) { // 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; // 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) { // 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; } 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; } } } } else { Console.WriteLine("No response received from SNMP agent."); } } target.Dispose(); } } } } |
C# SNMP 编程相关推荐
- C语言snmp编程视频,在Ubuntu18.04中关于C语言使用netsnmp进行snmp编程
前两天,发布了一篇关于Python使用netsnmp进行snmp编程的百家号文章,居然有不少人参看,阅读,因此顺便把C语言使用netsnmp的方法,说明一下供大家参考. 言归正传,进入主题,为了完整性 ...
- python netsnmp_在Ubuntu18.04中关于Python使用netsnmp进行snmp编程
关于snmp的开发,netsnmp目前的最新版本是5.7.3. 为了支持python的开发,网上的文章看了不少,走了不少弯路,所以总结一下,和大家共享. 第一部分: 安装snmp程序以及服务. 在Ub ...
- java 编写snmp_使用Java进行SNMP编程
简单的说,只需要以下几个步骤 1) 创建Snmp对象snmp 2) 创建CommunityTarget对象target,并指定community, version, address, timeout, ...
- C语言snmp编程视频,使用net-snmp API编程_C语言教程_C++教程_C语言培训_C++教程培训_C/C++频道_中国IT实验室...
在一个项目中使用了Redback SMS10000 的接入,作为附加要求,需要做一个snmp的接口程序,目的是起发送一个subscriber reauth 的 snmp 包给接入; 由于snmp的例程 ...
- 服务器代理设置与MIB信息获取实验报告,MIB浏览器的设计试验报告
一.实验目的 1.基本掌握了 MIB的结构: 2.掌握C++环境下SNMP编程的基本方法. 二.实验环境 1.VC++ 6.0 2.<Visual C++开发基于SNMP网络管理软件>书的 ...
- Snmp在Windows下的实现----WinSNMP编程原理
在Windows 下实现SNMP协议的编程,可以采用Winsock接口,在161,162端口通过udp传送信息.在Windows 2000中,Microsoft已经封装了SNMP协议的实现,提供了一套 ...
- 基于visual c++之windows核心编程代码分析(31)SNMP协议编程
SNMP(Simple Network Management Protocol,简单网络管理协议)的前身是简单网关监控协议(SGMP),用来对通信线路进行管理.随后,人们对SGMP进行了很大的修改,特 ...
- PYTHON黑帽编程1.5 使用WIRESHARK练习网络协议分析
Python黑帽编程1.5 使用Wireshark练习网络协议分析 1.5.0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks At ...
- Linux之socket套接字编程20160704
介绍套接字之前,我们先看一下传输层的协议TCP与UDP: TCP协议与UDP协议的区别 首先咱们弄清楚,TCP协议和UCP协议与TCP/IP协议的联系,很多人犯糊涂了,一直都是说TCP/IP协议与UD ...
最新文章
- MySQL事物系列:1:事物简介
- 两台路由器之间建立邻接关系的过程即报文信息交换过程
- 【2012百度之星/资格赛】H:用户请求中的品牌
- 神经网络那些事儿(二)
- Git使用教程:最详细、最傻瓜、最浅显、真正手把手教
- java中缓冲区和缓存_Java中的Google协议缓冲区
- Python 装饰器详解(下)
- 全新的企业通讯软件 FreeEIM 2.0
- h5移动端局部放大效果
- 在python不同版本下导入libvirt模块
- 经典配分函数公式以及量子统计形式
- traceroute、tracert服务的工作原理
- 字符数组与字符串 统计空格个数
- Windows下强制删除文件或文件夹
- TrueCrypt 使用经验[1]:关于加密算法和加密盘的类型
- mysql initialize 什么意思_mysql initialize
- 2021-2027全球与中国双联式过滤器外壳市场现状及未来发展趋势
- python自动化运维开发入门-张子夜-专题视频课程
- 小米电视4a刷鸿蒙,小米电视4A精简系统教程
- 20162316刘诚昊 课下排序测试