最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

引入

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

思路

我們可以使用Node.js的內(nèi)置模塊os,調(diào)用其中的networkInterfaces方法。該方法會返回一個包含網(wǎng)絡(luò)接口信息的數(shù)組對象,通過遍歷該數(shù)組對象,可以獲取到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"
        }]
}

封裝代碼

可以看到是個鍵值對的形式,所以我們直接獲取key,然后遍歷取值,再獲取對象中的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地址的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)electron獲取mac地址內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論

银川市| 涪陵区| 时尚| 桂阳县| 辛集市| 孟村| 无棣县| 喀喇| 唐海县| 黑山县| 普陀区| 淮南市| 长寿区| 建瓯市| 东宁县| 陆河县| 河曲县| 安远县| 五家渠市| 怀仁县| 精河县| 金阳县| 久治县| 夏津县| 东港市| 栖霞市| 麻城市| 珠海市| 徐水县| 富裕县| 龙口市| 安徽省| 景泰县| 山阴县| 澎湖县| 永德县| 来安县| 阿克陶县| 蓝田县| 仲巴县| 淅川县|