0

I am developing a SNMP utility in C# which can fetch data from a specified device OID using the SNMP version 1 packet format.

I have almost completed it but their is a problem that I am not able to resolve.

I am successfully querying one variable by sending a single "Get" packet but I need to query multiple variables by sending a single packet.

I tried it in this way:

//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

I googled a lot but could not find any thing relevant.

Johannes Kommer
  • 6,401
  • 1
  • 39
  • 45
  • 3
    Welcome to Stack Overflow! Have you tried reading the specification? Don't copy random code off the Internet and expect us to fix it. – dtb May 12 '12 at 09:32

1 Answers1

3

Parsing SNMP PDU from raw bytes, is never as simple as the code segment you pasted.

To seriously use SNMP in C#, you need to consider one of the following libraries,

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

Lex Li
  • 60,503
  • 9
  • 116
  • 147
  • Lex Li,Thanks for your reply!!! Actually, I have successfully parsed SNMP PDU from raw bytes, but only for "ONE" variable query. The requirement is for multiple variable query.I am not able to find the exact byte-format for multiple variable binding in single packet. – user1333218 May 13 '12 at 05:03
  • You asked for advice and the best advice really is to use a proper library and not try parse the raw apcket yourself. If you are really bored, take a look at the ASN.1 spec. It'll make your head spin. ;-) – Gene Vincent May 13 '12 at 07:21
  • Friends my problem is not so big. Everything is working fine when one query object per packet is used.But recently, I have come to know that a single packet(pdu) can have more than one query objects(variable bindings). But I couldnot find the exact packet format.I **just need the packet format for multiple variable binding** in single packet. – user1333218 May 13 '12 at 08:42
  • The code I have pasted above is starting from variable binding sequence _p[bytepos++] = 0x30;_ _p[bytepos++] = Convert.ToByte(6 + oid_len - 1 + 6 + oid_len2 - 1);_ Then first object is added e _p[bytepos++] = 0x30;_ till............ _p[bytepos++] = 0x00;_ after this is the second object. – user1333218 May 13 '12 at 08:54
  • If your goal is to write your own SNMP library instead of using others, a simple way to test compatibility is to generate message bytes from other libraries, and then compare with the ones generated by your code. Soon you will see the differences. It is not easy to describe ASN.1 and BER here. – Lex Li May 13 '12 at 09:31