0

我正在用 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

我用谷歌搜索了很多,但找不到任何相关的东西。

4

1 回答 1

3

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

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

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

于 2012-05-12T12:06:29.807 回答