如何使用oid设置snmp4jjava客户端的ipv6地址?

7gs2gvoe  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(270)

如何使用snmp4j和java客户端设置ipv6的ip地址?我可以用snmp4j设置ipv4ip地址。下面是使用snmp4j设置ipv4ip地址的代码快照。我对ipv6使用相同的方法,但没有给出正确的结果。如果我正在传递ipv6地址,即“20xx:04xx:00xx:40xx:0000:x:00xx". 则正在设置的值为xx.xx.xx.xx.00.xx.xx。它只是从每个八位字节中去掉前两个数字。就像从20xx年起,它删除了20个和它为所有人所做的相同的方式。

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;
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题