.NET C#使用 SNMP通讯 获取交换机数据

SNMP是简单网络管理协议(SNMP) 是专门设计用于在 IP 网络管理网络节点(服务器、工作站、路由器、交换机及HUBS等)的一种标准协议,它是一种应用层协议。

我之间搭建好了试验环境 大家也可以使用 eNsp或者真实的物理设备进行学习与测试

在这里 我就以我搭建的 eNsp虚拟环境进行说明 具体搭建配置过程 请看我的另一篇文章

拓扑图如下

在之前的搭建文章已经说过 在下面的 Cloud1 端连接的是 VMWare的虚拟网卡 这样我VM开的虚拟机内部就可以和这个网络通信,各个节点互通,在虚拟机内部运行C# .net SNMP测试程序连接这个网络的交换机设备获取数据

Core是核心交换机且配置DHCP
ACC 是接入层交换机
VLAN10 是部门A
VLAN20 是部门B
VLAN100 是与上层通信的
各个VLAN间三层互通我们我们就组成了一个小型的园区网络:
在这里插入图片描述

首先要使用SNMP通讯传输数据一定是要在设备端开启SNMP的功能
我这边要获取交换机的SNMP的数据就要在交换机端开启

华为交换机SNMP配置

snmp服务配置
交换机内设置snmp一般只需要启动snmp服务和配置团体名称,然后设置下版本就可以了

全局模式下,配置命令
1.启动snmp服务:

snmp-agent

2.设置团体名称:
snmp-agent community read cipher +团体名称(其中read为只读权限)

snmp-agent community write cipher +团体名称 (写的权限)

snmp-agent community write cipher public

3.设置联系方式:
snmp-agent sys-info contact +联系方式(可不配置,可以是电话等)

snmp-agent sys-info contact 123456789

4.设置设备位置:
snmp-agent sys-info location +地址名称(可不配置)

snmp-agent sys-inof location beijing

5.设置版本号
snmp-agent sys-info version +版本号(版本号打问号查看就可以了 all代表全部)

snmp-agent sys-info version all

当然版本也可以多个一起设置,例如:
v1和v3一起设置:

snmp-agent sys-info version v1 v3

这种配置体现的效果是:允许v1版本,默认允许v3版本

6.通过团体名称发送指定报文
使用某个团体名允许向网管工作站{NMS} ,x.x.x.x 发送Trap报文,命令如下:
snmp-agent target-host trap address udp-domain x.x.x.x(IP地址) params securityname +团体名称

我们按照上述描述在交换机端进行配置 我这里配置的是核心交换机

很简单 我们开启 snmp-agent 并且 设置团体名 运行所有SNMP版本 这样就配置好了
在这里插入图片描述
在这里插入图片描述

在虚拟机内部可以 ping 通 核心交换机 10.10.100.1

配置网管工具与交换机连接

网管和交换机通信使用SNMP协议,SNMP协议支持三个版本:SNMPv1、SNMPv2c和
SNMPv3。为了保证安全性,请使用SNMPv3连接网管和设备。
SNMPv1和SNMPv2c协议都是使用团体名进行认证的,配置方式类似。SNMPv3协议
使用用户名、密码进行认证,更加安全。
配置过程以MG-SOFT MIB Browser为例进行介绍。
安装并打开 MIB Browser
在这里插入图片描述

在 Remote SNMP agent 输入交换机的IP地址 再点击 后面的按钮进行配置团体名 点击OK
在这里插入图片描述

点击Walk 可以遍历整个 MIB树结构
在这里插入图片描述

提示是否重新更新当前的树结构 我们选 Yes to All
在这里插入图片描述

然后就会遍历所有节点并慢慢更新 MIB树
在这里插入图片描述

我们找个节点看一下里面的内容 atPhysAddress

在这里插入图片描述

我们也可以右键属性看到他的OID
在这里插入图片描述

可以直接点击 树形目录下面的枝叶 然后右键 Info 查看信息
例如我下面查看的 atPhysAddress的记录信息,对应ISO标准应该可以直接看到翻译过来的英文名
但不同品牌的设备有着私有的OID信息对于这种就需要 找到设备厂家提供的MIB库 在 MG-SOFT MIB Browser中进行导入操作才能看见
在这里插入图片描述

我们获取其他的OID也可以去比如华为的官网下载对应交换机型号的 MIB参考 里面有详细的介绍 各个OID的信息
也详细介绍了各个 OID相关联的关系
在这里插入图片描述

我这边使用C# 进行开发寻找.net 的SNMP封装好的类库可以直接使用这样就大大的方便了我们的开发
SnmpSharpNet 官网 http://snmpsharpnet.com/

大家尽量从官网的范例代码进行参考,否则在网上随便找代码的有很大的坑,

在官网中找到 范例的代码地址http://snmpsharpnet.com/index.php/walk-operation-with-snmp-version-1-and-2c/

程序如下

(SNMP V2c版本 批量获取示例)

SNMP Version 2 GET-BULK Example
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 = 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());
                                if (v.Value.Type == SnmpConstants.SMI_ENDOFMIBVIEW)
                                    lastOid = null;
                                else
                                    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.Close();
        }
    }
}

我们尝试获取一下
IP地址是交换机的Vlan100的地址
OID 是 1.3.6.1.2.1.3.1.1.2
在这里插入图片描述

很好数据没问题~
在这里插入图片描述

到此就结束啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值