Search code examples
javasnmpnet-snmpsnmp4jsnmp-trap

Get Target Ip Address From SNMP4j Trap Receiver


I create snmp trap receiver using snmp4j. I managed to receive the pdu and process it. I need the targeted ip address as my machine have multiple ip addresses. I could thinking either two ways:

  1. Retrieve entire command in my trap receiver which is "snmpset -v 1 -c M xx.xxx.x.x 1.3.6.1.4.1.161.3.6.37.2.1.3.2 i 2" so that I can get the ip address by removing other strings." But the only thing I could retrieve is only the variable, value and pdu type.
  2. Using method to retrieve the targeted ip address. But couldn't find one. I used event.getTransportMapping().getListenAddress() but getting 0.0.0.0/161 as I need to listen to many ip address. Used event.getPeerAddress() but getting ip address of the sender's machine.

Really appreciate for any kind of help. Thanks in advance.


Solution

  • Solved by:

    UdpAddress udpHostAdress = (UdpAddress) cmdRespEvent.getTransportMapping().getListenAddress();
    String ipAdd = udpHostAdress.getInetAddress().getHostAddress();
    System.out.println("Target IP = " + ipAdd);