NodeJS -离线时获取MAC地址

1rhkuytd  于 2023-01-25  发布在  Node.js
关注(0)|答案(3)|浏览(541)

this question in C#类似,在NodeJS中是否可以在断开网络连接时获取计算机的MAC地址?
我一直在使用macaddress模块,当用户连接到网络时,它工作得很好--但是如果用户断开连接,macaddress将不会返回某些系统上的任何地址。
我注意到用户离线/在线时os.networkInterfaces()之间的一些差异,以及Windows/Mac和节点v10/v12之间的行为差异。

4jb9z9bj

4jb9z9bj1#

我刚刚测试了getmac模块,离线(和在线)都很好。
你可以这样试试:

require('getmac').getMac(function(err,macAddress){
    if (err)  throw err
    console.log(macAddress) // 77:31:c2:c5:03:10
})

如果你不想使用一个模块,你也可以像这样询问每个mac地址接口(node〉= 0.11):

require('os').networkInterfaces()

然后根据需要解析它。
结果应如下所示:

{ lo0:
   [ { address: '::1',
       netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
       family: 'IPv6',
       mac: '00:00:00:00:00:00',
       scopeid: 0,
       internal: true },
     { address: '127.0.0.1',
       netmask: '255.0.0.0',
       family: 'IPv4',
       mac: '00:00:00:00:00:00',
       internal: true },
     { address: 'fe80::1',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: '00:00:00:00:00:00',
       scopeid: 1,
       internal: true } ],
  en0:
   [ { address: '10.3.162.15',
       netmask: '255.255.254.0',
       family: 'IPv4',
       mac: '77:31:c2:c5:03:10',
       internal: false } ],
  vboxnet0:
   [ { address: '192.168.33.1',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: '0a:00:27:00:00:00',
       internal: false } ],
  en3:
   [ { address: '10.3.32.45',
       netmask: '255.255.248.0',
       family: 'IPv4',
       mac: '0c:4d:e7:3d:3d:17',
       internal: false } ] }
7jmck4yq

7jmck4yq2#

目前面临着同样的情况。我结束了以下代码的基础上知道的IP地址的接口,我们已经知道。

var os = require('os');
    var getNetworkDataForThisIP = function(ipOfTheInterfaceWanted) {
        var networkInterfaces = os.networkInterfaces();
        var selectedInterfaceData;
        Object.keys(networkInterfaces).forEach((NetworkID, index, obj) => {
            networkInterfaces[NetworkID].forEach((data) => {
                if (data.family == "IPv4" && ipOfTheInterfaceWanted== data.address) {
                    //I created new Object because the NetworkID is not provided in the 'data' object
                    selectedInterfaceData = {
                        network: NetworkID,
                        address: data.address,
                        netmask: data.netmask,
                        family: data.family,
                        mac: data.mac,
                    };
                }
            });
        });
        return selectedInterfaceData;
    }

    var data = getNetworkDataForThisIP("192.168.5.1")
    console.log(data.network);
    console.log(data.address);
    console.log(data.netmask);
    console.log(data.family);
    console.log(data.mac);

您可以根据您已经知道的内容进行修改以满足您的需要。
实际上没有回答离线的问题,但您可以检查!!data的状态。

pqwbnv8z

pqwbnv8z3#

我推荐你使用流行的systeminformation库,它每周的下载量超过一百万次,无论你离线还是在线,它都能很好地工作。另外,你还可以访问系统的其他属性:
下面是获取MAC的示例:

const si = require('systeminformation');

si.networkInterfaces().then((data) => {
   console.log(data);
});

// Output ----------------------
[
  {
     iface: 'Wi-Fi',
     ifaceName: 'Intel(R) Dual Band Wireless-AC 7265',
     default: true,
     ip4: '192.168.13.112',
     ip4subnet: '255.255.255.0',
     ip6: '',
     ip6subnet: '',
     mac: 'gc:13:40:03:bx:as',
     internal: false,
     virtual: false,
     operstate: 'up',
     type: 'wireless',
     duplex: '',
     mtu: '',
     speed: 144.4,
     dhcp: true,
     dnsSuffix: '',
     ieee8021xAuth: 'Not defined',
     ieee8021xState: 'Disabled',
     carrierChanges: 0
  },
  {
     iface: 'Ethernet',
     ifaceName: 'Intel(R) Ethernet Connection (3) I218-LM',
     default: false,
     ip4: '',
     ip4subnet: '',
     ip6: '',
     ip6subnet: '',
     mac: '51:7g:9x:41:c6:aa',
     internal: undefined,
     virtual: false,
     operstate: 'down',
     type: 'wired',
     duplex: '',
     mtu: '',
     speed: 9223372036854.775,
     dhcp: true,
     dnsSuffix: '',
     ieee8021xAuth: 'Not defined',
     ieee8021xState: 'Disabled',
     carrierChanges: 0
  },
  {
     iface: 'Bluetooth Network Connection',
     ifaceName: 'Bluetooth Device (Personal Area Network)',
     default: false,
     ip4: '',
     ip4subnet: '',
     ip6: '',
     ip6subnet: '',
     mac: 'zc:13:30:28:ff:sf',
     internal: undefined,
     virtual: false,
     operstate: 'down',
     type: 'wired',
     duplex: '',
     mtu: '',
     speed: 3,
     dhcp: true,
     dnsSuffix: '',
     ieee8021xAuth: 'Not defined',
     ieee8021xState: 'Disabled',
     carrierChanges: 0
  }
];

相关问题