• 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 测试成功

  • 相关阅读:
    xmind 8 readme
    【08】英语词汇速记大全1词根词缀记忆法
    【20180312】2018年03月12日(随想)
    大佬说
    github私有库购买信息
    【01】用构造器创建函数中的小知识
    【01】魔芋使用MDN的一点点经验
    10.12 telnet:远程登录主机
    10.11 arping:发送arp请求
    10.10 traceroute:追踪数据传输路由状况
  • 原文地址:https://www.cnblogs.com/simadi/p/15047442.html
Copyright © 2020-2023  润新知