node.js IP 및 맥 가져오기

3152 단어
;
(function (win) {
    
    var os = require('os');
    var ifaces = os.networkInterfaces();

    function NetworkUtil() {
        var _self = this;
        
        this.getMac = function(family){
            var mac = '';
            Object.keys(ifaces).forEach(function (ifname) {
                
                ifaces[ifname].forEach(function (iface) {
                    if (family !== iface.family || iface.internal !== false) {
                        return;
                    }
                    mac = iface.mac;
                });
            });
            return mac;
        }

        this.getIP = function(family){
            var ip = '';
            Object.keys(ifaces).forEach(function (ifname) {
                
                ifaces[ifname].forEach(function (iface) {
                    if (family !== iface.family || iface.internal !== false) {
                        return;
                    }
                    ip = iface.address;
                });
            });
            return ip;
        }
        
    }
    win.global_NetworkUtil = new NetworkUtil();
})(window);
    

좋은 웹페이지 즐겨찾기