[SNMP4J] how should I modify this program using SNMP4J
Frank Fock
fock at agentpp.com
Mon Apr 17 00:20:57 CEST 2006
Hello,
If your program should be able to receive the trap it sends,
then you will have to rethink your code towards chronology
(and multi-threading).
You did not specify any port to listen for SNMP requests/traps
by setting a transport-mapping with a port != 0. So it cannot
receive any trap. Try using the
http://www.snmp4j.org/doc/org/snmp4j/Snmp.html#addNotificationListener(org.snmp4j.smi.Address,
org.snmp4j.CommandResponder)
method to add a trap listener port and transport mapping
combination. This is a convenience method.
Hope this helps.
Best regards,
Frank
guogang wrote:
>
> Hello,
> I am writing a SNMP trap receiver to do the following:
> 1. send a trap
> 2. receive it
> I have written a programa:
> ---------------------------------------------------------------------------------------
> public class testd implements CommandResponder, PDUFactory {
>
> private Address address = new UdpAddress("127.0.0.1/162");
> private OID oid = new OID("1.3.6.1.2.1.1");
> private CommunityTarget target = null;
> private OctetString community = new OctetString("public");
> public testd()
> {
> target = new CommunityTarget();
> }
>
> public static void main(String[] args) {
> try {
> testd snmp_ = new testd();
> PDU response = snmp_.send();
>
> } catch (Exception e) {
> System.out.println("ERROR");
> e.printStackTrace();
> }
> }
>
> public PDU send() throws IOException {
> Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
>
> target.setCommunity(new OctetString("public"));
> target.setVersion(SnmpConstants.version1); target.setAddress(address);
> target.setRetries(2);
> target.setTimeout(1000);
>
> snmp.listen();
>
> PDUv1 request = new PDUv1();
> request.setType(PDU.V1TRAP);
> request.setGenericTrap(PDUv1.COLDSTART);
> request.add(new VariableBinding(SnmpConstants.sysDescr,new OctetString("You System Description")));
> snmp.trap(request,target);
>
> CommandResponder trapPrinter = new CommandResponder()
> {
> public /*synchronized*/ void processPdu(CommandResponderEvent e) {
> PDU command = e.getPDU();
> if (command != null)
> {
> System.out.println("success");
> System.out.println(command.toString());
> }
> else
> {System.out.println("NULL ");}
> }
> };
> snmp.addCommandResponder(trapPrinter);
>
>
> System.out.println(request);
>
> snmp.close();
> return request;
> }
>
> public void processPdu(CommandResponderEvent event) {
> }
>
> public PDU createPDU(Target target) {
> PDU aa = null;
> System.out.println("display");
> aa.add(new VariableBinding(SnmpConstants.sysDescr,new OctetString("You System Description")));
> return aa ;
> }
> }
> ---------------------------------------------------------------------------------------
>
> but it can't receive SNMP traps,.
> Could anyone guide me as for how I should do?
> Thanks,
> Hust.gg
>
> _______________________________________________
> SNMP4J mailing list
> SNMP4J at agentpp.org
> http://lists.agentpp.org/mailman/listinfo/snmp4j
>
More information about the SNMP4J
mailing list