Skip to content

Instantly share code, notes, and snippets.

@akirad
Created April 24, 2016 10:56
Show Gist options
  • Save akirad/4807e0482423a442609930860771b5dc to your computer and use it in GitHub Desktop.
Save akirad/4807e0482423a442609930860771b5dc to your computer and use it in GitHub Desktop.
A sample to send a snmp trap data.
// A sample to send a trap data.
// Ref.
// http://jis.hatenablog.com/entry/2013/09/05/134001
// https://www.qoosky.net/techs/f8c35bb5d7
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class SnmpTrapClient {
public static void main(String args[]) {
try {
if (args.length != 1 || args[0].length() == 0) {
System.out.println("Need a trap send destination...");
System.exit(1);
}
InetAddress serverAddr = InetAddress.getByName(args[0]);
int trapRcvPort = 162;
// Create trap data.
byte sendData[] = {(byte)0x30, (byte)0x62, // Sequence type, Length from here: 98 byte.
(byte)0x02, (byte)0x01, (byte)0x01, // v2c
// Other data...
};
DatagramPacket dataPacket = new DatagramPacket(sendData, sendData.length, serverAddr, trapRcvPort);
// Send trap.
DatagramSocket dataSocket = new DatagramSocket();
dataSocket.send(dataPacket);
dataSocket.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment