Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@lzwme/get-physical-address

lzwme85MIT1.1.0TypeScript support: included

Try get the physical address(hardware MAC address) of the hosts network interfaces. Filter the virtual machine network card, VPN virtual network card, etc., and return the real MAC address information of the physical network card.

mac, mac-address, physical-address, visual-mac, physical, hardware, network interfaces, network card, arp, arp table, arp lookup

readme

@lzwme/get-physical-address

@lzwme/get-physical-address

build status NPM version node version license MIT

npm download GitHub issues GitHub forks GitHub stars

简体中文

Try get the physical address(hardware MAC address) of the hosts network interfaces. Filter the virtual machine network card, VPN virtual network card, etc., and return the real MAC address information of the physical network card.

Background

In Node.js or electron applications, it may be necessary to use MAC address and IP address as important marks to identify the uniqueness of the device, and use them as the basis for establishing the blacklist / whitelist mechanism of user equipment.

Via the API of os.networkInterfaces() you can easily get the information of the device network card.

However, when the virtual machine and VPN are enabled on the user's machine, the number of network cards returned by the API may be very large. At this time, it is necessary to identify the real physical network card to ensure that the information of the only physical network card can be accurately returned no matter whether the virtual network card is used or not, so as to avoid misjudgment caused by identification error.

Install

npm i @lzwme/get-physical-address
# use yarn
yarn add @lzwme/get-physical-address

Usage

Example:

import { getNetworkIFaceOne, getMac, isVirtualMac } from '@lzwme/get-physical-address';

getNetworkIFaceOne().then(item => {
  console.log(`isVirtualMac: ${isVirtualMac(item.mac, item.desc)}. the MAC address is ${item.mac}, the IP address is ${item.address}`);
});

getMac().then(mac => console.log(`the MAC address is ${mac}`));
getMac('en0').then(mac => console.log(`the MAC address for en0 is ${mac}`));

Example for some other API:

import { isMac, hasMac, isValidMac, isVirtualMac, formatMac, getAllPhysicsMac } from '@lzwme/get-physical-address';

isMac('aa-bb-cc-dd-ee-ff'); // true
hasMac('The MAC address is aa-bb-cc-dd-ee-ff'); // true
isMac('00:00:00:00:00:00'); // true
isValidMac('00:00:00:00:00:00'); // false
formatMac('AA-BB-CC-DD-EE-FF'); // aa:bb:cc:dd:ee:ff
isVirtualMac('00:0c:29:ae:ce'); // true

getAllMac().then(list => console.log(list));
getAllPhysicsMac('IPv4').then(list => console.log(list));

API

getMac

  • getMac(iface?: string): Promise<string>
  • getAllPhysicsMac(family?: 'IPv4' | 'IPv6'): Promise<string[]>
  • getAllMac(): string[] Filtered by internal=true and isZeroMac(macAdress)

getNetworkInterface

  • getNetworkIFaces(iface?: string, family?: 'IPv4' | 'IPv6'): Promise<os.NetworkInterfaceInfo[]>
  • getNetworkIFaceOne(iface?: string): Promise<os.NetworkInterfaceInfoIPv4 | os.NetworkInterfaceInfoIPv6>

arpTable

  • getArpTable(arpSstdout?: string)
  • getArpMacByIp(ip: string)
  • getArpIpByMac(mac: string)

utils

  • isMac(mac: string): boolean
  • hasMac(str: string): boolean
  • isZeroMac(mac: string): boolean
  • isValidMac(mac: string): boolean
  • formatMac(mac: string): string

Development

git clone https://github.com/lzwme/get-physical-address
yarn install
npm link
yarn dev

License

@lzwme/get-physical-address is released under the MIT license.

该插件由志文工作室开发和维护。

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

1.1.0 (2023-12-25)

Features

  • 新增 getArpTable 方法 (02fac9b)

Bug Fixes

  • 新增 vEthernet 虚拟网卡关键字 (0973735)

1.0.5 (2023-07-12)

Bug Fixes

  • 修复 wmic 获取的 mac 地址未格式化导致的描述过滤失效问题(close #1) (3b618ff)

1.0.4 (2023-07-03)

1.0.3 (2022-10-25)

Bug Fixes

  • 新增 Sangfor VPN 虚拟网卡识别规则 (3724712)

1.0.2 (2022-10-13)

Bug Fixes

  • 更新通过 wmic 获取描述信息的处理逻辑 (1ec85a2)

1.0.1 (2022-05-09)

Bug Fixes

  • fix for husky install scripts (3810f3b)