Error running SNMP with snmpsharpnet

3.5k Views Asked by At

I am running into issues running SNMP get commands with the snmpsharpnet library. I am working off of an example that they provide for running a simple get, but it errors out. I have tested running this OID against the box and I am able to get a response, but I can't with this program

My code looks like this:

try{
    SimpleSnmp snmp = new SimpleSnmp(HOST, COMMUNITY);

    Pdu pdu = new Pdu();
    //pdu.Type = SnmpConstants.GETNEXT; // type GETNEXT
    pdu.VbList.Add(".1.3.6.1.2.1.1.1.0");
    Dictionary<Oid, AsnType> result = snmp.Get(SnmpVersion.Ver2,pdu); //.GetNext(pdu);
    if (result == null){
        Console.WriteLine("Request failed.");
    }else{
        foreach (KeyValuePair<Oid, AsnType> entry in result)
        {
            Console.WriteLine("{0} = {1}: {2}", entry.Key.ToString(), SnmpConstants.GetTypeName(entry.Value.Type),
            entry.Value.ToString());
        }
    }
}catch (Exception ex){
    Console.WriteLine("Error: " + ex + Environment.NewLine + "-------------------------------------------------------");
}

The Error I receive looks like this:

A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in System.dll
A first chance exception of type 'SnmpSharpNet.SnmpException' occurred in SnmpSharpNet.dll
The thread 0xeec has exited with code 259 (0x103).

Thanks in advance!

1

There are 1 best solutions below

3
On BEST ANSWER

You're not receiving a response from the remote host for the request you're sending. This is the reason for the socket exceptions. There are 3 of them because the default for the SimpleSnmp class is to make 3 attempts to send & receive a response from the server.

If you set the Retry property of the snmp object to a higher number than 2, it will send out more requests and listen for more responses, generating more of these exceptions.

The standard behavior of snmp is not to generate any responses to requests that are either (a) malformed or (b) don't have the correct community string.

If you had shown what the resulting console output was from running this piece of code, I'm pretty certain it would have said Request failed.