0

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!

Lex Li
  • 60,503
  • 9
  • 116
  • 147
Ferbla
  • 51
  • 1
  • 11
  • You probably want to mention how you're getting the correct result from the system in question (e.g. snmpget). Make sure that the host & community strings match between the working example and the code supplied match. Check the `Valid` property of the `SimpleSnmp` instance. The default `pdu.Type` is `GET`, but to be safe, you should probably set it. – Anya Shenanigans Jun 06 '14 at 21:25
  • 1
    Can you add the output of the program? The `Console.WriteLine` should write a description of the error in the console. – user1793963 Jun 08 '14 at 21:12
  • The sharp-snmp tag is for #SNMP Library. Don't use it for snmpsharpnet, as they are completely different things. – Lex Li Jun 09 '14 at 02:39
  • The output I get is what is displayed up above in the original post, and in my terminal window I get the 'Request Failed.' message – Ferbla Jun 09 '14 at 12:48

1 Answers1

1

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.

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Thanks for the reply @Petesh. That is good to know, I was actually wondering about how one would go about changing that. I tried this morning changing the host and community for a different device, and I received information back on the first try. Then I switched back, triple checking the host and community values and I still receive the errors from the original post. – Ferbla Jun 09 '14 at 12:47
  • @Ferbla That indicates that your code is OK. From your symptoms it sounds like either (a) the server doesn't actually have an snmp service running, or (b) there is a firewall interfering with your communications to the server. You should check snmp access (1) from the same machine using a different snmp client (2) from a different machine. If it works from a different machine then it indicates connectivity/firewall issues. – Anya Shenanigans Jun 09 '14 at 12:57
  • thanks for your help. I moved this application to the production environment and it went through. It turns out to be a firewall issue. – Ferbla Jun 09 '14 at 13:41