1

How to set Ipv6 IP address using snmp4j with java client? I am able to set an ipv4 IP address with snmp4j. Below is the code snapshot for setting an ipv4 IP address with snmp4j. I am using same method for ipv6 but it's not giving proper results. If I am passing an ipv6 address i.e "20xx:04xx:00xx:40xx:0000:XXXX:00xx". Then the value which is being set is xx.xx.xx.xx.00.xx.xx. It just removing first two digits from every octet. Like from 20xx it removes 20 and the same way its done for all.

PDU pdu = SnmpSession.getPDU(OID_TO_SET_TFTPSERVER_IPADDRESS, "oa:be:09:ef"); //ipv4 address 
enter code here in hexa decimal format
SnmpSession.sendWrite(pdu, target);

public static PDU getPDU(String str , Object value ) {
    OID oid = new OID(str);
    final PDU pdu = new PDU();
    pdu.setType(PDU.SET);
    Variable var = null;
    try {
        var = SnmpTypeConverter.mapHexStringToVariable(value);
        
    } catch (Exception e) {
        logger.info(e.getMessage(), e);
    }
    pdu.add(new VariableBinding(oid, var));
    return pdu;
    
}

public static Variable mapHexStringToVariable(Object value) {
    String str = (String)value;
    Variable vb =  OctetString.fromHexString( str);
    return vb;
}

public static boolean sendWrite(final PDU pdu, Target mWriteTarget) throws InterruptedException, SnmpException {
    boolean isSetUP = true;
    try {
        Snmp mSnmp = new Snmp(new DefaultUdpTransportMapping());
        mSnmp.listen();
        final ResponseEvent responseEvt = mSnmp.send(pdu, mWriteTarget);
        if (Thread.currentThread().isInterrupted()) {
            throw new InterruptedException("SNMP write interupted");
        }

        final PDU response = responseEvt.getResponse();
        
        if (response != null) {
            if (response.getErrorStatus() != SnmpConstants.SNMP_ERROR_SUCCESS) {
                isSetUP = false;
            logger.info("Agent error code: " + response.getErrorStatus() + " - "
                    + response.getErrorStatusText() + " - " + response.getVariableBindings().toString());
            throw new SnmpException("Agent error code: "
                    + response.getErrorStatus() + " - "
                    + response.getErrorStatusText() + " - "
                    + response.getVariableBindings().toString());
        }

        if (response.getType() == PDU.REPORT) {
            final VariableBinding bind = response.get(0);
                logger.info("Report recieved: " + bind.toString());
        }
        return isSetUP;
    }
    } catch (final IOException ex) {
        logger.info(ex.getMessage(), ex);
        logger.info("Error While setting the value");
    }
    return isSetUP;
}

0 Answers0