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
            });
        }
    }        
  • 相关阅读:
    巨人的崛起 Android操作系统发展历程
    iPhone 4S定位功能加强 支持GLONASS系统
    android子线程更新UI,与主Thread一起工作
    什么在阻碍移动互联网的普及?
    WEB.CONFIG 配置详解
    解决在aspx页面上进行传中文参数时会出现乱码问题
    VS2005水晶报表注册码
    windows2003应用程序池假死的问题
    浅谈ASP.NET内部机制
    合理建立数据库索引
  • 原文地址:https://www.cnblogs.com/zh33gl/p/4831827.html
Copyright © 2011-2022 走看看