SNMP Trap Listener Using #SNMP Library

Overview

Microsoft provides us SNMP.EXE and SNMPTRAP.EXE services to listen to SNMP (Simple Network Management Protocol) traps. Using these services, we can listen to SNMP traps but since security is a big concern, these services fail to listen to SNMPv3 traps which are mainly developed for security reasons.

However, a lot of libraries are available on the internet for this purpose but most of them are paid. 

Then, what next…………………….?

#SNMP Library

#SNMP Library (sharp SNMP Library)

#SNMP Library is a free and open source tool which is developed in C#. It comes under MIT license for development.

Types of SNMP traps

Basically, the SNMP trap is categorized into three major types.

  1. SNMP v1 was the first version of SNMP.
  2. SNMP v2c improved the error handling and supports 64-bit counters.
  3. SNMP v3 is the newest version of SNMP. Its primary feature is enhanced security.

For more information, just click on the below link - https://docs.sharpsnmp.com/

Overview of project

We are going to create a Windows Service in C# which will be hosted on a system and it will continuously receive the SNMP traps.

Step 1

Open Visual Studio, go to File > New, and select Project. Now, select a new project from the dialog box and select “Windows Service” and click the "OK" button. The name of my Windows Service is SnmpTrapReceiverWS.

#SNMP Library

Rename the Service1.cs file to SnmpTrapReceiverWS. This is a best practice for a good developer. This is not mandatory if you don’t want to rename it, then keep it as is.

Step 2

Add a reference to SharpSnmpLib.dll file to this solution. Here, we are going to use SharpSnmpLib for SNMP trap receiver. Right-click on the Reference folder and browse the SharpSnmpLib.dll to your solution.

Note
Request you to download the DLL from the download section of this article.

#SNMP Library

Step 3

Write the below code on OnStart and OnStop, as shown below.

  1. protected override void OnStart(string[] args) {  
  2.     try {  
  3.         //Debbugin windows service.  
  4.         System.Diagnostics.Debugger.Launch();  
  5.         //Required only if we are going to received SNMP v3 Trap.  
  6.         //If we are going to received SNMP V3 trap then un-comment below code.  
  7.         //var users = new UserRegistry();  
  8.         //users.Add(new OctetString("SecurityUserName"),  
  9.         // new DefaultPrivacyProvider(new MD5AuthenticationProvider(new OctetString("AuthenticationPassword"))));  
  10.         //lst = new Listener { Users = users };  
  11.         lst = new Listener();  
  12.         lst.AddBinding(new IPEndPoint(IPAddress.Any, 162)); //IP address of listener system  
  13.         lst.MessageReceived += Listener_MessageReceived;  
  14.         lst.StartAsync();  
  15.     } catch (Exception ex) {}  
  16. }  
  17. private static void Listener_MessageReceived(object sender, MessageReceivedEventArgs e) {  
  18.     File.AppendAllText("c:/temp/servicelog.log""Version :" + e.Message.Version + "\n");  
  19.     File.AppendAllText("c:/temp/servicelog.log""Version :" + e.Message.Scope.Pdu.Variables[4].Data.ToString() + "\n");  
  20. }  
  21. protected override void OnStop() {  
  22.     lst.Stop();  

#SNMP Library

Step 4

Now, host the service as a Windows Service. Use the below command to host service as Windows Service.

sc create "SnmpListenerWS" binpath= "D:\Articles\SNMP\SnmpTrapReceiverWS\SnmpTrapReceiverWS\bin\Debug\SnmpTrapReceiverWS.exe"

Note -
In this command, there is a space after binpath like.

sc create "SnmpListenerWS" binpath= <space>"<path of service>"

#SNMP Library

Step 5

  1. Now, open the Run prompt and open MSC. Then, start the SnmpTrapReceiverWS service.

    #SNMP Library

    #SNMP Library

  2. When the service will start, it will ask for debugging because of the below line of code.

    System.Diagnostics.Debugger.Launch();

    #SNMP Library

  3. If the above line is commented, then it is not going to ask you for debugging.

    Click on Yes, debug SnmpTrapReceiverWS.exe, and start the debugging.

    #SNMP Library

Step 6

  1. Download the SNMP trap simulator (SNMP trap sender) and open the config file from the below path to change the IP address wherever we want to send the trap from the simulator.

    Go to the below path.

    <driveNameWhereThisSimulatorDowloaded>\Norscan Instruments\Norscan Instruments\Norscan OAU Simulator\nssnmptrap.exe.Config

    #SNMP Library

    Note
    You can download it from the download section.

  2. Change the IP address in Config.exe, click on nssnmptrap.exe from the same path where the simulator is downloaded.

    #SNMP Library

  3. Click on the Send button from the simulator. A debug point will hit automatically and we can fetch the trap based on requirements as shown below.

    #SNMP Library

    Note
    Keep in mind that our Windows Service SnmpTrapReceiverWS is in running mode.

  4. We can check the log from the below path.

    C:\TEMP\servicelog.log

    #SNMP Library

Conclusion

In this article, we learned about #SNMP Library, SNMP trap, how to create, host, debug a Windows Service in C#. You can download complete code from the download section of this article.

For more information about #SNMP (SharpSNMP) Library request, you can refer to the below link.

https://docs.sharpsnmp.com/

Keep cheering!!!