简体   繁体   English

C#SNMP陷阱接收器

[英]C# SNMP trap receiver

I need to trap SNMP and have done the following code, I'm getting the data correctly but the objectID is coming out garbled, is there any reason for this 我需要捕获SNMP并完成以下代码,我正确获取数据但是objectID出现乱码,是否有任何原因

       int port=162;
         UdpClient listener;
        IPEndPoint groupEP;
        byte[] packet = new byte[1024];
        int commlength, miblength, datatype, datalength, datastart, Objecttype, Objectlength;
        int objectstart;
        string objectid;

        string output;

        Console.WriteLine("Initializing SNMP Listener on Port:" + port + "...");

       // try
       // {
            listener = new UdpClient(port);
            groupEP = new IPEndPoint(IPAddress.Any, port);

            while (true)
            {
                Console.WriteLine("Waiting for messages....");
                packet = listener.Receive(ref groupEP);
                Console.WriteLine("Processing new message...");
                if (packet.Length != null)
                {
                Console.Out.WriteLine("New message from {0} :\n {1}\n", groupEP.ToString(), packet);
                if (packet[0] == 0xff)
                {
                    Console.WriteLine("Invalid Packet");
                    return;
                    }

                commlength = Convert.ToInt16(packet[6]);
               miblength = Convert.ToInt16(packet[10 + commlength]);

               Objecttype = Convert.ToInt16(packet[30 + commlength + miblength]);
                Objectlength = Convert.ToInt16(packet[31 + commlength + miblength]);
                objectstart = 32 + commlength + miblength;
                datatype = Convert.ToInt16(packet[26+ Objecttype + Objectlength+commlength+ miblength]);
                datalength = Convert.ToInt16(packet[27 + Objecttype + Objectlength + commlength + miblength]);
                datastart = 28 + Objecttype + Objectlength + commlength + miblength;
                    output = Encoding.ASCII.GetString(packet, datastart, datalength);
                    objectid = Encoding.ASCII.GetString(packet, objectstart , Objectlength);
                Console.WriteLine("sysLocation - Datatype: {0}, Value: {1}", datatype,output);
                Console.WriteLine(objectid);


                }


            }

The data is stored as string hex, would the objectid be stored the same way as it contains '.' 数据存储为字符串十六进制,objectid将以与包含'。'相同的方式存储。 I know I can use existing libraries (freely available on the net), but I have just created myself an oppurtunity to learn. 我知道我可以使用现有的图书馆(网上免费提供),但我刚刚创造了一个学习的机会。

Please advise. 请指教。

have a look at this SNMP lib http://sourceforge.net/projects/snmpsharpnet/files/ . 看看这个SNMP lib http://sourceforge.net/projects/snmpsharpnet/files/

you do not have to reinvent the wheel 你不必重新发明轮子

Example in C# and VB.Net with SnmpSharpNet. 使用SnmpSharpNet在C#和VB.Net中的示例。 Click here 点击这里

Now in 2019 I would rather go for #SNMP since it's constantly developed based on Nuget package last date published. 现在在2019年,我宁愿选择#SNMP,因为它是基于Nuget包发布的最新日期不断开发的。 Has up-to-date docs and this neat article which describes exactly what you want to achieve. 拥有最新的文档和这篇简洁的文章 ,它准确描述了您想要实现的目标。 It looks like it's working with .NET Core as well. 看起来它也适用于.NET Core

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM