lextudio / sharpsnmplib

C# SNMP Library (#SNMP) - Open source SNMP implementation for C# and .NET
https://sharpsnmp.com
MIT License
358 stars 151 forks source link

How to get values like sysObjectId from private MIBS #687

Closed ExtraBinoss closed 2 weeks ago

ExtraBinoss commented 2 weeks ago

Hello,

I am currently working with Lextm #SNMP. Currently I have the need of parsing the sysObjectId ( .1.3.6.1.2.1.1.2) in order to get the model name of my switch.

I have a MIB browser which can load MIB's to find the correct modelName. This works flawlessly. Now I have tried using the snmpget CLI, in order to assure that I could make it work. It did work with

SNMPv2-MIB::sysObjectID.0 = OID: HM2-PRODUCTS-MIB::rsp
➜  snmpget -v2c -c public -m ALL {IP} .1.3.6.1.2.1.1.2.0;

I used -m ALL flag in order to make it work, or else it wouldn't show me right name

this is the response without -m ALL flag.
iso.3.6.1.2.1.1.2.0 = OID: iso.3.6.1.4.1.248.11.2.1.2

I have moved all my needed mibs inside my /usr/share/snmp/mibs/ folder (there is no sub folders or anything. Only MIB files.)

However when using the LEXTM #Snmp lib, it seems that the oid is not found correctly, it is show the same thing as snmpget without -m flag.

My implementation is simple :

public List<Variable> SnmpGet(string ip, string oid)
        {
            var endpoint = new IPEndPoint(IPAddress.Parse(ip), 161);
            var variables = new List<Variable> { new Variable(new ObjectIdentifier(oid)) };

            for (int i = 0; i < retries; i++)
            {
                try
                {
                    var result = Messenger.Get(VersionCode.V1, endpoint, new OctetString(community), variables, timeout);
                    return (List<Variable>)result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error querying IP {ip}, OID {oid}, attempt {i + 1}: {ex.Message}");
                    if (i == retries - 1)
                    {
                        throw;
                    }
                }
            }

            return new List<Variable>();
        }

Output was :

1.3.6.1.4.1.248.11.2.1.2

I wonder how we can fix this problem, if possible. I have tried : "dotnet build" and dotnet run, to make sure the new MIBS are loaded, but to no success.

Kind regards,

lextm commented 2 weeks ago

MIB support is covered by #SNMP Pro. You can learn more from https://pro.sharpsnmp.com