码迷,mamicode.com
首页 > Web开发 > 详细

解决net-snmp正确输出MAC地址和判断空的IP地址

时间:2015-09-23 13:17:46      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:

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
        });
    }
}        

 

解决net-snmp正确输出MAC地址和判断空的IP地址

标签:

原文地址:http://www.cnblogs.com/zh33gl/p/4831827.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!