This project has moved. For the latest updates, please go here.

Writing a simple SNMP Get command-line program ?

Topics: usage (how to ...)
Jun 17, 2008 at 3:45 PM
Hi there.

Thank you so much for doing the work you do with the #SNMP library.  Having a simple DLL I can reference in my Visual Studio 2008 for C# is so much easier than messing around with the stuff that I saw for free online and since I'm a hobbiest programmer (possibly making the leap to something more meaningful in the future) the commercial libraries are a little out of my reach of course (often $500 to $1200).

If you could show me a simple program which does the following in command-line, it will give me the kick-start I need to get moving:

  • Command-Line prompts user for an OID to specify (Console.Readline()) - I can do this
  • Command-Line prompts user for an IP address to specify (Console.Readline()) - I can do this 
  • Logic takes advantage of the libraries you built to do a simple SNMP Get (assume Int or String is the value - assume 500ms is the time-out) -- this is where I need help
  • Command-Line spits out the get result - I can do this

I just need a little jump start and I know this sounds very newbie-ish (I'm admitting that).  I'm sure I wouldn't be the only person would would find this helpful.  Even just well-commented C# code would be enough.  I'd find this primer infinitely valuable.  Tell me how much I should "donate" to make this happen :), thanks.

Jun 19, 2008 at 12:28 AM
Okay, never mind.  I didn't realize that the source code had an SNMPGET test case, so just used that to reference and I'm okay now.  This is extremely fast.  Really nice work!  I might have to do some playing around if there's time-out thresholds I can specify, so if you have any recommendations there (or could let me know what the default timeout threshold is - e.g. 240ms) that would be helpful. Marked as answer by lextm on 10/6/2013 at 9:16 PM
Coordinator
Jun 25, 2008 at 12:27 PM
Edited Jun 25, 2008 at 12:28 PM
Hi, glad to see you find the sample :-) Because the samples right now are small and simple, I only distribute them in source code form. Later I am going to write more typical samples and bundle them into both binary and source code form.

About the timeout, I don't know what's the best. #SNMP uses 5 seconds as default (defined in Manager.cs int _timeout). This is also  default value used in other SNMP utilities. In my practice, usually 5 seconds is fine for lab setup, while on deployment sites the value varies. Therefore, any SNMP utilities ought to provide the users a dialog or command line switch to change the timeout value. Just don't use a fixed value. :-)
Jul 8, 2008 at 3:37 PM

public

IList<Variable> Send(int timeout)

 

{

 

byte[] bytes = _bytes;

 

 

IPEndPoint agent = new IPEndPoint(_agent, 161);

 

udp.Send(bytes,bytes.Length,agent);

 

IPEndPoint from = new IPEndPoint(IPAddress.Any,0);

 

 

IAsyncResult result = udp.BeginReceive(null, this);

 

result.AsyncWaitHandle.WaitOne(timeout,

false);

 

 

if (!result.IsCompleted)

 

{

 

throw SharpTimeoutException.Create(_agent, timeout);

 

}
-----
This always ending up in SharpTimeoutException, meaning a timeout occured. I changed the timeout values to 5 minutes, still nothing happens, and it times out. I am using your test console applications. Any help will be appreciated

Coordinator
Jul 9, 2008 at 4:22 AM
Hi. timeout exception does not always mean the "real" timeout. According to my latest findings, sometimes a bad agent implementation may drop SNMP v1 request and return no reply. Then #SNMP also throws a timeout exception. Of course, there may be other special cases.

I guess you are in such a special case, so I can only provide some test cases you may try.

  • When you call Manager.Get (or Set), please specify you'd like to use SNMP v2 (only available in latest source code in CodePlex repository, not in 0.9 release).
  • Try another community string, "public", "private", or the one you can find in the agent documents.

Because SNMP v2 provides more error information than v1, this time you may get a more detailed error description.

By the way, may you provide some information about the SNMP agent you use?

Marked as answer by lextm on 10/6/2013 at 9:15 PM
Jul 9, 2008 at 3:39 PM
Actually I was running one of your test programs (TestWalk). That means, it was going against the localhost(127.0.0.1). I hope this answers your question.