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;
}
The
-Oxflag that you are asking about doesn't affect the SNMP transaction, only the interpretation of the received data. In short, instead of usingvarBinding.toValueString(), you want to be usingOctetString.toHexString(). Based on the documentation, it looks like you can extract the value as aVariable, and then cast it to anOctetString. Here's an example: