zoukankan      html  css  js  c++  java
  • 解决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
            });
        }
    }        
  • 相关阅读:
    基于矩阵式产品管理的奖金如何发放?
    再谈技术开发项目与产品开发项目的差异
    如何培养合格的产品经理?
    技术规划变革管理——共创力典型咨询案例
    研发人员任职资格管理深圳公开课成功举办!
    深圳市共创力《产品需求挖掘和规划》深圳公开课!(2019.12.6~7)
    技术规划与路标开发实践公开课在深圳成功举办!
    产品路标开发是企业持续成功的关键路径
    什么是技术规划(TPP)?
    什么是测试系统工程师(TSE)?
  • 原文地址:https://www.cnblogs.com/zh33gl/p/4831827.html
Copyright © 2011-2022 走看看