解决net-snmp正确输出MAC地址和判断空的IP地址
function readVarbinds (buffer, varbinds) { buffer.readSequence (); while (1) { buffer.readSequence (); var oid = buffer.readOID (); var type = buffer.peek (); if (type == null) break; var value; if (type == ObjectType.Boolean) { value = buffer.readBoolean (); } else if (type == ObjectType.Integer) { value = readInt (buffer); } else if (type == ObjectType.OctetString) { value = buffer.readString (null, true); //添加 if(value.length === 6){ //关键点:mac转换成字符串的话是乱码。 //如果str的长度是2表示2个中文,如果长度是6的话,检查是否是字母或数字 var str = value.toString(); if(str.length === 6 && !/[0-9A-Fa-f]{6}/.test(str)){ value = value.toString('hex').toUpperCase(); } } } else if (type == ObjectType.Null) { buffer.readByte (); buffer.readByte (); value = null; } else if (type == ObjectType.OID) { value = buffer.readOID (); } else if (type == ObjectType.IpAddress) { var bytes = buffer.readString (ObjectType.IpAddress, true); if (bytes.length != 4) //添加 value = ''; //注释 //throw new ResponseInvalidError ("Length '" + bytes.length + "' of IP address '" + bytes.toString ("hex") + "' is not 4"); else value = bytes[0] + "." + bytes[1] + "." + bytes[2] + "." + bytes[3]; } else if (type == ObjectType.Counter) { value = readUint (buffer); } else if (type == ObjectType.Gauge) { value = readUint (buffer); } else if (type == ObjectType.TimeTicks) { value = readUint (buffer); } else if (type == ObjectType.Opaque) { value = buffer.readString (ObjectType.Opaque, true); } else if (type == ObjectType.Counter64) { value = readUint64 (buffer); } else if (type == ObjectType.NoSuchObject) { buffer.readByte (); buffer.readByte (); value = null; } else if (type == ObjectType.NoSuchInstance) { buffer.readByte (); buffer.readByte (); value = null; } else if (type == ObjectType.EndOfMibView) { buffer.readByte (); buffer.readByte (); value = null; } else { throw new ResponseInvalidError ("Unknown type '" + type + "' in response"); } varbinds.push ({ oid: oid, type: type, value: value }); } }