snmpsharpnet的Trap实例
首先附上两个程序:
1:发送端程序Trapsend
using System; using System.Collections.Generic; using System.Linq; using System.Text; using SnmpSharpNet; namespace Trapsend { class Program { static void Main(string[] args) { //while(true) Trap(); } static void Trap() { //trap发送测试 TrapAgent agent = new TrapAgent(); // Variable Binding collection to send with the trap // 可以发送各种类型的数据 VbCollection col = new VbCollection(); col.Add(new Oid("1.3.6.1.2.1.1.1.0"), new OctetString("Test string")); col.Add(new Oid("1.3.6.1.2.1.1.2.0"), new Oid("1.3.6.1.9.1.1.0")); col.Add(new Oid("1.3.6.1.2.1.1.3.0"), new TimeTicks(2324)); col.Add(new Oid("1.3.6.1.2.1.1.4.0"), new OctetString("2016-1-2,17:53:2.0")); // Send the trap to the localhost port 162 agent.SendV1Trap(new IpAddress("localhost"), 162, "public", new Oid("1.3.6.1.2.1.1.1.0"), new IpAddress("127.0.0.1"), SnmpConstants.LinkUp, 0, 13432, col); } } }
2:接收端程序
using System; using System.Net; using System.Net.Sockets; using SnmpSharpNet; namespace traprecv { class Program { static void Main(string[] args) { // 接收的还是UDP类型的socket包 // Construct a socket and bind it to the trap manager port 162 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 162); EndPoint ep = (EndPoint)ipep; socket.Bind(ep); //绑定 // Disable timeout processing. Just block until packet is received (超时处理) socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 0); bool run = true; int inlen = -1; // 数据的长度 while (run) { byte[] indata = new byte[16 * 1024]; // 收到的数据 // 16KB receive buffer int inlen = 0; IPEndPoint peer = new IPEndPoint(IPAddress.Any, 0); EndPoint inep = (EndPoint)peer; try { inlen = socket.ReceiveFrom(indata, ref inep); // 计算数据的长度 } catch (Exception ex) { Console.WriteLine("Exception {0}", ex.Message); inlen = -1; } if (inlen > 0) { // Check protocol version int int ver = SnmpPacket.GetProtocolVersion(indata, inlen);// 计算收到的SNMP包的版本号 if (ver == (int)SnmpVersion.Ver1) // 版本1处理方式 { // Parse SNMP Version 1 TRAP packet SnmpV1TrapPacket pkt = new SnmpV1TrapPacket(); pkt.decode(indata, inlen); // 解码,获得trap包的一些基本信息 Console.WriteLine("** SNMP Version 1 TRAP received from {0}:", inep.ToString());// agentIP Console.WriteLine("*** Trap generic: {0}", pkt.Pdu.Generic); Console.WriteLine("*** Trap specific: {0}", pkt.Pdu.Specific); Console.WriteLine("*** Agent address: {0}", pkt.Pdu.AgentAddress.ToString()); Console.WriteLine("*** Timestamp: {0}", pkt.Pdu.TimeStamp.ToString()); Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count); Console.WriteLine("*** VarBind content:"); foreach (Vb v in pkt.Pdu.VbList) { Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString()); } Console.WriteLine("** End of SNMP Version 1 TRAP data."); } else { // Parse SNMP Version 2 TRAP packet SnmpV2Packet pkt = new SnmpV2Packet(); // 版本2处理方式 pkt.decode(indata, inlen); Console.WriteLine("** SNMP Version 2 TRAP received from {0}:", inep.ToString()); if ((SnmpSharpNet.PduType)pkt.Pdu.Type != PduType.V2Trap) { Console.WriteLine("*** NOT an SNMPv2 trap ****"); } else { Console.WriteLine("*** Community: {0}", pkt.Community.ToString()); Console.WriteLine("*** VarBind count: {0}", pkt.Pdu.VbList.Count); Console.WriteLine("*** VarBind content:"); foreach (Vb v in pkt.Pdu.VbList) { Console.WriteLine("**** {0} {1}: {2}", v.Oid.ToString(), SnmpConstants.GetTypeName(v.Value.Type), v.Value.ToString()); } Console.WriteLine("** End of SNMP Version 2 TRAP data."); } } } else { if (inlen == 0) Console.WriteLine("Zero length packet received."); } } } } }
有了程序还是不够的,任然需要启动windows的SNMP服务
开启SNMP服务:我的电脑》管理》服务》SNMP Service
A.添加区域,如图
B.添加陷阱,如图
最后附上最后的效果图:
2021-07--23 测试成功
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2013-07-23 VB高清图标制作方法