1. 程式人生 > >node.js獲取ip及mac

node.js獲取ip及mac

node.js res ces FN elf mil func require net

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

node.js獲取ip及mac