Skip to main content

SNMP Trap Generator using SNMP4J

This project is set up as a maven project where snmp4j library is mentioned as a dependency.

<dependency>
    <groupId>org.snmp4j</groupId>
    <artifactId>snmp4j</artifactId>
    <version>1.10.1</version>
</dependency>

This program will send SNMPv2 Trap message to any ip address mentioned. A SNMP Trap receiver program or application which is listening on port 162 would be able to receive the message.

 import java.io.IOException;  
 import org.snmp4j.CommunityTarget;  
 import org.snmp4j.PDU;  
 import org.snmp4j.Snmp;  
 import org.snmp4j.mp.SnmpConstants;  
 import org.snmp4j.smi.Address;  
 import org.snmp4j.smi.OID;  
 import org.snmp4j.smi.OctetString;  
 import org.snmp4j.smi.TimeTicks;  
 import org.snmp4j.smi.UdpAddress;  
 import org.snmp4j.smi.Variable;  
 import org.snmp4j.smi.VariableBinding;  
 import org.snmp4j.transport.DefaultUdpTransportMapping;  
 public class TrapGenerator {  
      /**  
       * @param args  
       * @throws IOException   
       */  
      public static void main(String[] args) throws IOException {  
           // Create PDU using SNMP4J       
         PDU trap = new PDU();  
         trap.setType(PDU.TRAP);  
         //Give some OID as it doesn't matter in this example  
         OID oid = new OID("1.3.6.1.2.1.1");  
         trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));  
         trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(1000)));  
         trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("My PC Windows 7 8GB RAM ")));   
         //Add Payload - here add your data  
         Variable var = new OctetString("My system is in trouble");       
         trap.add(new VariableBinding(oid, var));       
         // Specify receiver  
         Address targetaddress = new UdpAddress("144.158.6.59/162");  
         CommunityTarget target = new CommunityTarget();  
         target.setCommunity(new OctetString("public"));  
         target.setVersion(SnmpConstants.version2c);  
         target.setAddress(targetaddress);  
         // Send Trap to the address mentioned  
         Snmp snmp = new Snmp(new DefaultUdpTransportMapping());  
         snmp.send(trap, target, null, null);       
      }  
 } 

 To test this program I have downloaded Trapreceiver application from http://www.trapreceiver.com/ and this application captures Trap messages on the system where it runs.

Open TrapReceiver and then run the Java program. You will see Trap message as below.

Hope this helps.

Comments

  1. The CDC reported that a small portable generator will produce the carbon-monoxide level of six idling cars, a reality that surprises many consumers.generatorguides.net

    ReplyDelete
  2. Gangaur Realtech is a professionally managed organisation specializing in real estate services where integrated services are provided by professionals to its clients seeking increased value by owning, occupying or investing in real estate. best generator for travel trailer

    ReplyDelete
  3. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates. best generator for travel trailer

    ReplyDelete

Post a Comment

Popular posts from this blog

Create SNMP Client in JAVA Using SNMP4j

Please follow the below mentioned steps to write a very simple SNMP4J application using Java and eclipse IDE. First you need to download a copy of SNMP4J.jar. You can get it from http://www.snmp4j.org/html/download.html Then you need to decide how you are going to test your application. Check whether you have access to any real network devices like Cisco routers or Juniper routers or whether you are aware of any SNMP agent running on any of your servers.There is nothing to worry if you cannot find one , you can start SNMP agent on your desktop machine as well. Here I am giving an example of how you can use your Windows machine to test your application by starting the SNMP service on the machine. I am using a Windows XP machine and below are the steps needed to start SNMP agent on the machine. 1. Go to Start->Control Panel -> Add or Remove Programs -> Add or Remove Windows Components      Select or Check "Management and Monitoring Tools". ...

Symantec Interview questions for the role of a Senior Software Developer C++

Below I have mentioned a few interview questions for the role of a Senior Software Engineer at Symantec. They were looking for a Senior person with mainly Windows API programming experience. I had 7 rounds of interview including a 'lunch with Manager' and a Director's interview. I have mentioned the questions below. I hope this would help some Developers looking for Symantec Interview Questions. Let me know your comments... C++/ OOPS Questions Rate yourself in C++ The interviewer wrote a  class with 2 members , a char* and a HANDLE  and asked me to complete the class. He was mainly looking for proper assignment operator and copy constructors. What is polymorphism What is abstract class and when to use abstract class How is virtual function implemented What is vptr, is vptr created per class or per object What is strlen_s() call would do? When to throw or catch an exception? Explain a few Design Patterns? Where did you use singleton and Factory patterns? ...

How to create a minikube single node cluster for learning Kubernetes

In this post I will explain how to setup a minikube single node kubernetes cluster using AWS EC2 instance which would help anyone who is trying to learn kubernetes and also help them to gain practical knowledge in kubernetes by running kubernetes commands, creating kubernetes objects etc. Minikube is a single node kubernetes cluster which means a kubernetes cluster with only one node that is a single VM. Minikube is only used for learning purposes and it is not an alternative for a real kubernetes cluster and should not be used for development and production usage. In this example I have launched an AWS EC2 instance with below configuration where I will install minikube and related tools. AWS EC2 Instance Configuration AMI: Ubuntu Free tier eligible 64 bit Instance type : t2-large ( For me t2-small or t2-micro is giving performance issues due to less memory) Once the EC2 instance is up and running, login to the instance using below command on terminal. If you are using wi...