C#SNMP陷阱接收器 [英] C# SNMP trap receiver

查看:194
本文介绍了C#SNMP陷阱接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要捕获SNMP并完成以下代码,我正确获取数据但是objectID出现乱码,有什么理由这个



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

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);


                }
            }







数据存储为字符串十六进制,objectid将以与包含'。'相同的方式存储。我知道我可以使用现有的库(网上免费提供),但我刚刚创造了一个学习的机会。



请注意。




The data is stored as string hex, would the objectid be stored the same way as it contains '.' I know I can use existing libraries (freely available on the net), but I have just created myself an oppurtunity to learn.

Please advise.

推荐答案

OID已编码。微软说这关于OID编码:



OBJECT IDENTIFIER数据类型被编码为TLV三元组,以Tag值0x06开头。点分十进制的每个整数对象标识符(OID)根据以下规则进行编码:



文章的其余部分:

OBJECT IDENTIFIER(Windows) [
The OID is encoded. Microsoft says this about OID encoding:

"The OBJECT IDENTIFIER data type is encoded into a TLV triplet that begins with a Tag value of 0x06. Each integer of a dotted decimal object identifier (OID) is encoded according to the following rules:"

The rest of the article :
OBJECT IDENTIFIER (Windows)[

这篇关于C#SNMP陷阱接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆