2

I am trying to create an SNMP client in JAVA.

SNMPManager client = new SNMPManager("udp:73.251.102.191/162");

This is good for an ipv4 address, but when I am trying to do the same with ipv6 addresses it throws the below error.

Error:

---------------Exception in thread "main" org.snmp4j.MessageException: No route to host
at org.snmp4j.MessageDispatcherImpl.sendPdu(Unknown Source)
at org.snmp4j.Snmp.sendMessage(Unknown Source)
at org.snmp4j.Snmp.send(Unknown Source)
at org.snmp4j.Snmp.send(Unknown Source)
at snmpTrial1.SNMPManager.get(SNMPManager.java:55)
at snmpTrial1.SNMPManager.getAsString(SNMPManager.java:45)
at snmpTrial1.SNMPManager.main(SNMPManager.java:34)

Code: This code works for ipv4 addresses but not ipv6

public class SNMPManager {
Snmp snmp = null;
String address = null;
public SNMPManager(String add)
{
    address = add;
}
public static void main(String[] args) throws IOException {
    SNMPManager client = new SNMPManager("udp:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX/162"); // ipv6 address
    client.start();
    String sysDescr = client.getAsString(new OID(".1.3.6.1.4.1.4115.1.20.1.1.3.62.4.1.1.1"));
    System.out.println(sysDescr);
}
private void start() throws IOException {
    TransportMapping transport = new DefaultUdpTransportMapping();
    snmp = new Snmp(transport);
    transport.listen();
}

public String getAsString(OID oid) throws IOException {
    ResponseEvent event = get(new OID[] { oid });
    return event.getResponse().get(0).getVariable().toString();
}
public ResponseEvent get(OID oids[]) throws IOException {
    PDU pdu = new PDU();
    for (OID oid : oids) {
        pdu.add(new VariableBinding(oid));
    }
    pdu.setType(PDU.GET);
    ResponseEvent event = snmp.send(pdu, getTarget(), null);
    if(event != null) {
        return event;
    }
    throw new RuntimeException("GET timed out");
}
private Target getTarget() {
    Address targetAddress = GenericAddress.parse(address);
    CommunityTarget target = new CommunityTarget();
    target.setCommunity(new OctetString("xxxxx"));
    target.setAddress(targetAddress);
    target.setRetries(2);
    target.setTimeout(2000);
    target.setVersion(SnmpConstants.version2c);
    return target;
}

}

Anil
  • 420
  • 2
  • 16
  • Could you please update your question with more code to help us visualize where the error could be occurring and why? – Evan Bechtol Jul 13 '15 at 15:04
  • 1
    please edit your question to include the content of your comments. This belongs as part of the question **not** a comment – Evan Bechtol Jul 13 '15 at 15:59
  • 1
    I am not pointing fingers, I am helping you by making your question of the proper format to receive an answer. In it's original state, it was likely to be closed because there was not enough contextual information. – Evan Bechtol Jul 13 '15 at 16:14
  • The exception suggests there is no route from the adapter IP address to the target IP address. I would investigate with OS tools like traceroute. – McDowell Jul 19 '15 at 20:16

2 Answers2

0

if your OS is MAC OSX then Java 7 and 8 has a bug related to IPv6 and socket connections. here is the link: jvm bug Java 6 seems to work fine.

Aleksey
  • 149
  • 4
0

I found the solution. For ipv6 address we should have the format like below GenericAddress.parse("udp:[XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX]/162");

This worked for me.

Bala
  • 1
  • 1