[SNMP4J] Help getting example traps to work

Muthu Vellayan muthu.vellayan at gmail.com
Tue Jun 9 00:24:53 CEST 2009


Version #1 -- similar to javadoc.
I can see that "targetAddress variable" is not used in the progam.  Is this
why the msg is getting ignored?

public static void main(String[] args) throws Exception {
    Address targetAddress = GenericAddress.parse("udp:10.10.25.111/162");
    TransportMapping transport = new DefaultUdpTransportMapping();
    USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3
                           .createLocalEngineID()), 0);
    SecurityModels.getInstance().addSecurityModel(usm);
    Snmp snmp = new Snmp(transport);

    snmp.getUSM().addUser(
           new OctetString("SecureUser"),
           new UsmUser(new OctetString("SecureUser"), AuthMD5.ID,
                       null,null,null));

    CommandResponder trapPrinter = new CommandResponder() {
       public synchronized void processPdu(CommandResponderEvent e) {
           PDU command = e.getPDU();
           if (command != null) {
               System.out.println(command.toString());
           }
         }
        };
    snmp.addCommandResponder(trapPrinter);
    transport.listen();
    Thread.sleep(300 * 1000);
}
Version #2 -- similar to some of the posts on the bb ...
but I get "Cannot assign requested address: Cannot bind" when trying to
instantiate DefaultUdpTransportMapping on the 2nd line in this method.


public static void main(String[] args) throws Exception {
    UdpAddress udpAddress = new
UdpAddress(Inet4Address.getByName("10.10.25.111"), 162);
    TransportMapping transport = new DefaultUdpTransportMapping(udpAddress);
    USM usm = new USM(SecurityProtocols.getInstance(),
                       new OctetString(MPv3.createLocalEngineID()),
                       0);
    SecurityModels.getInstance().addSecurityModel(usm);
    Snmp snmp = new Snmp(transport);

    snmp.getUSM().addUser(
            new OctetString("SecureUser"),
            new UsmUser(new OctetString("SecureUser"), AuthMD5.ID,
                                  null,null,null));

    CommandResponder trapPrinter = new CommandResponder() {
        public synchronized void processPdu(CommandResponderEvent e) {
           PDU command = e.getPDU();
           if (command != null) {
               System.out.println(command.toString());
             }
          }
       };

       snmp.addCommandResponder(trapPrinter);
       transport.listen();
       Thread.sleep(300 * 1000);
}



More information about the SNMP4J mailing list