Visual C # 2015 calls the Snmpsharpnet library to implement a simple SNMP element query

Source: Internet
Author: User
Tags snmp

A few SNMP libraries were found at the start of the survey,

One is NET-SNMP, this seems to be a lot of Linux

One is Microsoft's own winsnmp, this no example, not very good operation

One is snmpsharpnet, some examples are better,

The code to read Udpindatagrams is implemented using the Snmpsharpnet example:

To put SnmpSharpNet.dll in the project directory of Visual C # 2015, and then add this DLL to the project browser reference, see here, reference, right-click, add Reference, browse to DLL

1 usingSystem;2 usingSystem.Net;3 usingsnmpsharpnet;4 5 namespaceSnmpget6 {7     class Program8     {9         Static voidMain (string[] args)Ten         { One             //SNMP Community Name AOctetString Community =NewOctetString (" Public"); -  -             //Define Agent Parameters Class theAgentparameters param =Newagentparameters (community); -             //Set SNMP version to 1 (or 2) -Param. Version =Snmpversion.ver1; -             //Construct the agent address object +             //IpAddress class is easy-use here because -             //It'll try to resolve constructor parameter if it doesn ' t +             //parse to an IP address AIpAddress Agent =NewIpAddress ("192.168.0.10"); at  -             //Construct Target -Udptarget target =NewUdptarget (IPAddress) agent,161, -,1); -  -             //Pdu class used for all requests -PDU PDU =NewPdu (pdutype.get); inPdu. Vblist.add ("1.3.6.1.2.1.7.1.0");//Udpindatagrams -  to             //Make SNMP request +Snmpv1packet result =(snmpv1packet) target. Request (PDU, param); -  the             //If result is a null then agent didn ' t reply or we couldn ' t parse the reply. *             if(Result! =NULL) $             {Panax Notoginseng                 //ErrorStatus Other and then 0 is a error returned by -                 //The agent-see snmpconstants for error definitions the                 if(result.) Pdu.errorstatus! =0) +                 { A                     //Agent reported an error with the request theConsole.WriteLine ("Error in SNMP reply. Error {0} index {1}", + result. Pdu.errorstatus, - result. Pdu.errorindex); $                 } $                 Else -                 { -                     //Reply variables is returned in the same order as they were added the                     //To the vblist -Console.WriteLine ("Sysdescr ({0}) ({1}): {2}",WuyiResult. pdu.vblist[0]. Oid.tostring (), theSnmpconstants.gettypename (result. pdu.vblist[0]. Value.Type), -Result. pdu.vblist[0]. Value.tostring ()); Wu                 } -             } About             Else $             { -Console.WriteLine ("No response received from SNMP agent."); -             } - Target. Close (); A         } +     } the}

The output results are as follows:

Wireshark of packets sent and received:

Get-request

Get-response

Next, you need to see how to get the network topology ...

Visual C # 2015 calls the Snmpsharpnet library to implement a simple SNMP element query

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.