package com.adventnet.snmp.scripting; import com.adventnet.snmp.snmp2.SnmpPDU; import com.adventnet.snmp.snmp2.SnmpAPI; import com.adventnet.snmp.snmp2.SnmpException; import com.adventnet.snmp.snmp2.SnmpSession; import com.adventnet.snmp.snmp2.UDPProtocolOptions; import java.io.*; public class CustomTrapForwarder implements ScriptHandler { SnmpPDU pdu; String hostName; int Port; public CustomTrapForwarder(SnmpPDU tpdu) { pdu = tpdu; hostName = "172.21.160.89"; Port = 8005; } public void execute() { TrapForwarder fTrap = new TrapForwarder(pdu,hostName,Port);//pdu,hostname,port value fTrap.sendTrap(); } public void setFilePath(String path) { } } class TrapForwarder { SnmpAPI api; SnmpSession session; SnmpPDU trappdu; String host; int port; public TrapForwarder(SnmpPDU pdu, String hostname, int port) { this.host = hostname; this.port = port; this.trappdu= pdu; } public void sendTrap() { System.out.println("Send Trap Method"); // Start SNMP API api = new SnmpAPI(); // Open session session = new SnmpSession(api); // set community if (trappdu.getCommunity() != null) session.setCommunity(trappdu.getCommunity()); try { // set remote host UDPProtocolOptions udpOpt = new UDPProtocolOptions(host); // set remote port try { if (port != 0) { udpOpt.setRemotePort(port); } else { udpOpt.setRemotePort(162); } } catch(Exception ex) { System.err.println("Invalid port: " + port); } trappdu.setProtocolOptions(udpOpt); try { // open session session.open(); // Send PDU session.send(trappdu); } catch (SnmpException e1) { System.err.println("Sending PDU"+e1.getMessage()); } // close session session.close(); // stop api thread api.close(); } catch(Exception ex) { System.out.println("Exception "+ex.getMessage()); } } }