SNMP PDU中的变量绑定

时间:2012-05-12 09:27:56

标签: c# udp snmp packet pdu

我正在使用C#开发一个SNMP实用程序,它可以使用SNMP版本1数据包格式从指定的设备OID获取数据。

我差不多已经完成了,但他们是一个我无法解决的问题。

我通过发送一个“Get”数据包成功查询一个变量,但我需要通过发送一个数据包来查询多个变量。

我用这种方式试了一下:

//variable bindings
p[bytepos++] = 0x30; //variable bindings sequence
p[bytepos++] = Convert.ToByte(6 + oid_len - 1 + 6 + oid_len2 - 1); // Size of variable binding
p[bytepos++] = 0x30; //first variable bindings sequence
p[bytepos++] = Convert.ToByte(4 + oid_len - 1); // size
p[bytepos++] = 0x06; //Object type
p[bytepos++] = Convert.ToByte(oid_len - 1 ); //length

//Start of MIB
p[bytepos++] = 0x2b;

for (i = 2; i < oid_len; i++)
    p[bytepos++] = Convert.ToByte(oid[i]);

p[bytepos++] = 0x05; //Null object value
p[bytepos++] = 0x00; //Null

//start of second variable bindings sequence
p[bytepos++] = 0x30; //Second variable bindings sequence
p[bytepos++] = Convert.ToByte(4 + oid_len2 - 1); // size
p[bytepos++] = 0x06; //Object type
p[bytepos++] = Convert.ToByte(oid_len2 - 1); //length

//Start of MIB
p[bytepos++] = 0x2b;

//Place MIB array in packet
for (i2 = 2; i2 < oid_len2; i2++)
    p[bytepos++] = Convert.ToByte(oid2[i2]);

p[bytepos++] = 0x05; //Null object value
p[bytepos++] = 0x00; //Null

我搜索了很多但找不到相关内容。

1 个答案:

答案 0 :(得分:3)

从原始字节解析SNMP PDU,绝不像您粘贴的代码段那么简单。

要在C#中认真使用SNMP,您需要考虑以下库之一,

http://www.lextm.com/2007/12/product-review-snmp-libraries-for-net.html