欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

electron中獲取mac地址的實(shí)現(xiàn)示例

 更新時(shí)間:2023年12月14日 11:27:01   作者:依星net188.com  
在基于Electron的應(yīng)用中,有一個(gè)業(yè)務(wù)需求是獲取物理網(wǎng)卡的Mac地址以用于客戶機(jī)唯一性識(shí)別,本文主要介紹了electron中獲取mac地址的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下

引入

為了方便做單點(diǎn)登錄,我們往往需要使用某個(gè)唯一標(biāo)識(shí)來標(biāo)記客戶端的設(shè)備,mac地址就是一個(gè)還不錯(cuò)的選擇

思路

我們可以使用Node.js的內(nèi)置模塊os,調(diào)用其中的networkInterfaces方法。該方法會(huì)返回一個(gè)包含網(wǎng)絡(luò)接口信息的數(shù)組對(duì)象,通過遍歷該數(shù)組對(duì)象,可以獲取到Mac地址,我們先看下調(diào)用得到的響應(yīng)內(nèi)容

import { networkInterfaces } from "os";
console.log(JSON.stringify(networkInterfaces()));

{
        "Ethernet0": [{
                "address": "fe80::803c:6a7b:14b0:f652",
                "netmask": "ffff:ffff:ffff:ffff::",
                "family": "IPv6",
                "mac": "00:0c:29:ea:41:55",
                "internal": false,
                "cidr": "fe80::803c:6a7b:14b0:f652/64",
                "scopeid": 7
        }, {
                "address": "192.168.213.154",
                "netmask": "255.255.255.0",
                "family": "IPv4",
                "mac": "00:0c:29:ea:41:55",
                "internal": false,
                "cidr": "192.168.213.154/24"
        }],
        "Loopback Pseudo-Interface 1": [{
                "address": "::1",
                "netmask": "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
                "family": "IPv6",
                "mac": "00:00:00:00:00:00",
                "internal": true,
                "cidr": "::1/128",
                "scopeid": 0
        }, {
                "address": "127.0.0.1",
                "netmask": "255.0.0.0",
                "family": "IPv4",
                "mac": "00:00:00:00:00:00",
                "internal": true,
                "cidr": "127.0.0.1/8"
        }]
}

封裝代碼

可以看到是個(gè)鍵值對(duì)的形式,所以我們直接獲取key,然后遍歷取值,再獲取對(duì)象中的mac地址即可:

/**獲取mac地址信息 */
export const getMacAddress = function (): string {
  const interfaces = networkInterfaces();
  let macAddress = "";
  for (const interfaceName of Object.keys(interfaces)) {
    const interfaceInfos = interfaces[interfaceName];

    if (interfaceInfos) {
      for (const interfaceInfo of interfaceInfos) {
        if (interfaceInfo.mac && interfaceInfo.mac !== "00:00:00:00:00:00") {
          macAddress = interfaceInfo.mac;
          break;
        }
      }
    }

    if (macAddress.length > 0) break;
  }

  return macAddress;
};

到此這篇關(guān)于electron中獲取mac地址的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)electron獲取mac地址內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評(píng)論