如何使用oid使用Snmp4J Java客户端设置ipv6地址?

问题描述

如何在Java客户端上使用snmp4j设置ipv6 IP地址?我可以使用snmp4j设置ipv4 IP地址。以下是使用snmp4j设置ipv4 IP地址的代码快照。我对ipv6使用了相同的方法,但未给出正确的结果。如果我传递的是ipv6地址,即“ 20xx:04xx:00xx:40xx:0000:XXXX: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;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...