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 (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.
- SNMP v1 was the first version of SNMP.
- SNMP v2c improved the error handling and supports 64-bit counters.
- 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.
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.
Step 3
Write the below code on OnStart and OnStop, as shown below.
- protected override void OnStart(string[] args) {
- try {
-
- System.Diagnostics.Debugger.Launch();
-
-
-
-
-
-
- lst = new Listener();
- lst.AddBinding(new IPEndPoint(IPAddress.Any, 162));
- lst.MessageReceived += Listener_MessageReceived;
- lst.StartAsync();
- } catch (Exception ex) {}
- }
- private static void Listener_MessageReceived(object sender, MessageReceivedEventArgs e) {
- File.AppendAllText("c:/temp/servicelog.log", "Version :" + e.Message.Version + "\n");
- File.AppendAllText("c:/temp/servicelog.log", "Version :" + e.Message.Scope.Pdu.Variables[4].Data.ToString() + "\n");
- }
- protected override void OnStop() {
- lst.Stop();
- }
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>"
Step 5
- Now, open the Run prompt and open MSC. Then, start the SnmpTrapReceiverWS service.
- When the service will start, it will ask for debugging because of the below line of code.
System.Diagnostics.Debugger.Launch();
- 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.
Step 6
- 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
Note
You can download it from the download section.
- Change the IP address in Config.exe, click on nssnmptrap.exe from the same path where the simulator is downloaded.
- 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.
Note
Keep in mind that our Windows Service SnmpTrapReceiverWS is in running mode.
- We can check the log from the below path.
C:\TEMP\servicelog.log
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!!!