calmh / node-snmp-native

Native Javascript SNMP library for Node.js
MIT License
252 stars 66 forks source link

Broken buffer for ".1.3.6.1.2.1.2.2.1.6" (if address) #41

Closed dr-dimitru closed 8 years ago

dr-dimitru commented 8 years ago

Expected: c8:60:0:df:8:61 Got:

{ 
  type: 4,
  value: '�`\u0000�\ba',
  oid: [ 1, 3, 6, 1, 2, 1, 2, 2, 1, 6, 2 ],
  valueRaw: <Buffer c8 60 00 df 08 61>,
  valueHex: 'c86000df0861', // Decoded HEX is 졠ßࡡ
  requestId: 502795267,
  receiveStamp: 1458171248188,
  sendStamp: 1458171248131
}

Response from snmpwalk:

IF-MIB::ifPhysAddress.2 = STRING: c8:60:0:df:8:61

Code:

session.getSubtree({
  oid: '.1.3.6.1.2.1.2.2.1.6'
}, function(error, varbinds) {
  if (!error && varbinds.length) {
    varbinds.forEach(function(vb) {
      console.log(vb);
    });
  }
});

Or am I doing something wrong?

calmh commented 8 years ago

snmpwalk is being smart here, as the returned data is an octet-string with the (binary) contents c8 60 00 df 08 61 (the MAC address), which you see in the valueRaw and valueHex fields. You probably want to use one of those directly.

dr-dimitru commented 8 years ago

Not a problem at all with:

valueHex.match(/.{1,2}/g).join(':');

But it just doesn't seems right, as it exactly return a string: IF-MIB::ifPhysAddress.2 = STRING: c8:60:0:df:8:61

calmh commented 8 years ago

It doesn't, snmpwalk is simplifying things for you. The actual type returned according the MIB is a PhysAddress:

PhysAddress ::=
    OCTET STRING
-- This data type is used to model media addresses.  For many
-- types of media, this will be in a binary representation.
-- For example, an ethernet address would be represented as
-- a string of 6 octets.

The actual octet string returned is not "c8:60:0:df:8:61", it is the six bytes c8 60 00 df 08 61.

dr-dimitru commented 8 years ago

@calmh Thank you for explanation, appreciated.