javasnmpsnmp4j

How can I use SNMP4J with Java to run snmpwalk with the -Ox parameter to force raw values?


I have found with some devices (mainly cisco) that they sometimes return some interesting values when running an snmpwalk. I have found specifically that they can change a HEX value to ASCII.

I'm able to pull back the raw values by using the following cmd passing through -Ox as a parameter: snmpwalk -Os **-Ox** -c <community> -v 2c <ip_address> <OID>= Hex-STRING: <result_as_hex>

^^^I would like to do that with snmp4j if possible.

The following cmd results in the same as the java code using snmp4j: snmpwalk -Os -c <community> -v 2c <ip_address> <OID> = STRING: <result_as_ASCII_value>

Many thanks in advance! J

I get the same response when I try to use snmp4j even though I am trying to get the VariableBinding.toValueString:

public Map<String, String> doWalk(String tableOid, Target target) throws IOException {
        log.info("doWalk({}, {})", tableOid, target);
        Map<String, String> result = new TreeMap<>();
        TransportMapping<? extends Address> transport = new DefaultUdpTransportMapping();
        
        log.info(transport.getListenAddress().toString());
        Snmp snmp = new Snmp(transport);
        
        transport.listen();
        
        TreeUtils treeUtils = new TreeUtils(snmp, new DefaultPDUFactory());
        List<TreeEvent> events = treeUtils.getSubtree(target, new OID(tableOid));
        
        if (events == null || events.isEmpty()) {
            log.info("Error: Unable to read table...");
            return result;
        }

        log.info("event size is {}", events.size());
        for (TreeEvent event : events) {
            if (event == null) {
                continue;
            }
            if (event.isError()) {
                log.info("Error: table OID [{}] {}", tableOid, event.getErrorMessage());
                continue;
            }
            
            VariableBinding[] varBindings = event.getVariableBindings();
            if (varBindings == null || varBindings.length == 0) {
                continue;
            }
            for (VariableBinding varBinding : varBindings) {
                if (varBinding == null) {
                    continue;
                }

                result.put("." + varBinding.getOid().toString(), varBinding.toValueString());
            }
        }
        snmp.close();

        return result;
    }

Solution

  • The -Ox flag that you are asking about doesn't affect the SNMP transaction, only the interpretation of the received data. In short, instead of using varBinding.toValueString(), you want to be using OctetString.toHexString(). Based on the documentation, it looks like you can extract the value as a Variable, and then cast it to an OctetString. Here's an example:

    Variable value = varBinding.getVariable();
    if (!value.getSyntaxString().equals("OCTET_STRING")) {
        // Throw an exception or something
    }
    
    OctetString octet_string = (OctetString) value;
    String hex = octet_string.toHexString();