Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am developing a Snmp utility in C# which can fetch data from a specified Oid of a device.I am using Snmp version1 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 single "Get" packet but I need to query multiple variables by sending single packet.

I tried it in this way:
C#
//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 couldnot find any thing relevent.Please Help!!
Posted
Comments
SASS_Shooter 14-May-12 10:50am    
Is there a reason you don't use existing API's out there to simplify the SNMP coding? I think API's like SimpleSNMP can help. I think it is as simple as sending the request to device 0 in the chain and it is treated as a query for all settings, but cannot remember it exactly.

1 solution

To get multiple variables in one binding, you need to append OID's one after another and then you must adjust length bytes in the packet.

Following is the dump of SNMPv1 GetRequest. There are five OIDs packed in a single packet. Highlighted in red is community string(public) and underlined are the first, third and fifth OID.

0000 30 82 01 21 02 01 00 04 06 70 75 62 6c 69 63 a0
0010 82 01 12 02 02 4e 9b 02 01 00 02 01 00 30 82 01
0020 04 30 18 06 14 2b 06 01 04 01 ab 57 01 0b 03 02
0030 01 02 05 01 07 03 31 2d 31 05 00 30 18 06 14 2b
0040 06 01 04 01 ab 57 01 0b 03 02 01 02 05 01 08 03
0050 31 2d 31 05 00 30 18 06 14 2b 06 01 04 01 ab 57
0060 01 0b 03 02 01 02 05 01 09 03 31 2d 31 05 00 30
0070 18 06 14 2b 06 01 04 01 ab 57 01 0b 03 02 01 02
0080 05 01 02 03 31 2d 32 05 00 30 18 06 14 2b 06 01
0090 04 01 ab 57 01 0b 03 02 01 02 05 01 03 03 31 2d
00a0 32 05 00 ED 52

Let take the third OID:
18 06 14 2b 06 01 04 01 ab 57 01 0b 03 02 01 02 05 01 08 03 31 2d 31 05 00

Where,
18 is Length of that OID (in Hex). Here 24 bytes
14 2b ... 31 05 00 is OID

30 is seperator. You can see 30 at the start of every OID.
 
Share this answer
 
Comments
Member 7881518 13-Jun-12 2:17am    
This is what I did and got the result but I could not find time to update my answer here.Thanks ShaminaMina for such a clear explanation!
ShaminaMina 13-Jun-12 2:24am    
:-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900