Giter VIP home page Giter VIP logo

node-macaddress's Introduction

node-macaddress

Build Status

Retrieve MAC addresses in Linux, OS X, and Windows.

A common misconception about MAC addresses is that every host had one MAC address, while a host may have multiple MAC addresses – since every network interface may have its own MAC address.

This library allows to discover the MAC address per network interface and chooses an appropriate interface if all you're interested in is one MAC address identifying the host system (see API + Examples below).

A common misconception about this library is that it reports the mac address of the client that accesses some kind of backend. It does not. Reporting the mac address is not in any way similar to reporting the IP address of the client that accesses your application. This library reports the mac address of the server the application is running on. This useful for example for distributed/scaled applications, for example when generating UUIDs.

Also it seems to be worth noting that this library is not intended to be used in a browser. There is no web api reporting the mac address of the machine (and that is a good thing).

Features:

  • works on Linux, Mac OS X, Windows, and on most UNIX systems.
  • node ≥ 0.12 and io.js report MAC addresses in os.networkInterfaces() this library utilizes this information when available.
  • also features a sane replacement for os.networkInterfaces() (see API + Examples below).
  • works with stoneage node versions ≥ v0.8 (...)
  • Promise support

Usage

npm install --save macaddress
var macaddress = require('macaddress');

API + Examples

(async)  .one(iface, callback) → string
(async)  .one(iface)           → Promise<string>
(async)  .one(callback)        → string
(async)  .all()                → Promise<{ iface: { type: address } }>
(async)  .all(callback)        → { iface: { type: address } }
(sync)   .networkInterfaces()  → { iface: { type: address } }

.one([iface], callback)

Retrieves the MAC address of the given iface.

If iface is omitted, this function automatically chooses an appropriate device (e.g. eth0 in Linux, en0 in OS X, etc.).

Without iface parameter:

macaddress.one(function (err, mac) {
  console.log("Mac address for this host: %s", mac);  
});

or using Promise

macaddress.one().then(function (mac) {
  console.log("Mac address for this host: %s", mac);  
});
→ Mac address for this host: ab:42:de:13:ef:37

With iface parameter:

macaddress.one('awdl0', function (err, mac) {
  console.log("Mac address for awdl0: %s", mac);  
});

or using Promise

macaddress.one('awdl0').then(function (mac) {
  console.log("Mac address for awdl0: %s", mac);  
});
→ Mac address for awdl0: ab:cd:ef:34:12:56

.all(callback)

Retrieves the MAC addresses for all non-internal interfaces.

macaddress.all(function (err, all) {
  console.log(JSON.stringify(all, null, 2));
});

or using Promise

macaddress.all().then(function (all) {
  console.log(JSON.stringify(all, null, 2));
});
{
  "en0": {
    "ipv6": "fe80::cae0:ebff:fe14:1da9",
    "ipv4": "192.168.178.20",
    "mac": "ab:42:de:13:ef:37"
  },
  "awdl0": {
    "ipv6": "fe80::58b9:daff:fea9:23a9",
    "mac": "ab:cd:ef:34:12:56"
  }
}

.networkInterfaces()

A useful replacement of os.networkInterfaces(). Reports only non-internal interfaces.

console.log(JSON.stringify(macaddress.networkInterfaces(), null, 2));
{
  "en0": {
    "ipv6": "fe80::cae0:ebff:fe14:1dab",
    "ipv4": "192.168.178.22"
  },
  "awdl0": {
    "ipv6": "fe80::58b9:daff:fea9:23a9"
  }
}

node-macaddress's People

Contributors

alexander-taran avatar alinalexandru avatar diastremskii avatar doublechuang avatar jamestalmage avatar jungwoo-an avatar logikgate avatar micaelmbagira avatar mo4islona avatar philraj avatar roebuk avatar scravy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-macaddress's Issues

Release a new version

Please release a new version that contains the fix for the global addresses leak.

Wrong Address

>  var gc                 = require('macaddress');
undefined
> gc.networkInterfaces()
{ wlan0: 
   { ipv4: '169.254.126.229',
     mac: 'b8:27:eb:a5:16:d4',
     ipv6: 'fe80::e1bf:48cf:786c:ee08' } }

But my net is:



wlan0     Link encap:Ethernet  HWaddr b8:27:eb:a5:16:d4  
          inet addr:192.168.1.149  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::e1bf:48cf:786c:ee08/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:47240 errors:0 dropped:37 overruns:0 frame:0
          TX packets:26971 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:17212206 (16.4 MiB)  TX bytes:13238658 (12.6 MiB)

Works on Localhost but not when deployed

I get the same macaddress of: 02:b0:82:ce:73:ec
regardless of which device I login to. My app is deployed on Amazon EC2 and I use cloudflare. Is there a possibility that cloudflare may have interfered?

macaddress.one reporting virtualbox port

On linux mint 19.3, node 12.16.1 -

On my desktop until recently, macaddress.one reported the address from the ethernet port used for external networking.

However, the latest version reports the address from a virtualbox port, vboxnet0, of "0a:00:27:00:00:00" (the usual for virtualbox).

Could macaddress.one recognize and not use the virtualbox address?

Does not return mac address if cable is unplugged.

On Windows 10, if the network cable is unplugged it won't return back that interface.

This is a known issue with the Node.js os.networkInterfaces:
nodejs/node#498

In windows if I do ipconfig /all it returns even those interfaces where the cable is unplugged.

What I tried:
macaddress.all(function (err, macs) {
});

Here macs won't contain the one without a plugged cable.

Doesn't working with node 8.1.x

.one(callback) = return a mac that doesn't exists in my pc

.one('en1', callback) = return the right mac but it repeats every 1 second, indefinitely

.all(callback) = print the correct ip of the active interface but the wrong mac, and repeats every 1 second, indefinitely

I'm running macOS Sierra 10.12.5 in an old macbook pro

Add FreeBSD to known platforms

unix.js works well with FreeBSD (At least on version 11).

os.platform() returns freebsd.

Maybe it could be added to the list of known Unixes to avoid the unknown platform warning?

Cut new release with Typescript definitions

Seems like a recent commit added Typescript definitions but no release was cut after? Could we have a new release for this so I don't need to copy/paste the definitions?

Error on Ubuntu 20.04 with wifi off

Hi! Firstly, thanks for this great library!

I got this error using macaddress 0.5.2 on Ubuntu 20.04, but only when I turn off wifi . It looks to me like the output of a command isn't being parsed properly:

Failed to get MAC address [
  "Error: invalid iface: 'enx00e04d3a478a\n" +
    'lo\n' +
    "wlp0s20f3' (must conform to reg exp //^[a-z0-9]+$/i/)\n" +
    '    at Object../node_modules/macaddress/lib/getmacaddress.js.module.exports [as getMacAddress] (http://localhost:3000/account/index.js:102475:18)\n' +
    ...

The code that give this error looks like this:

import { one as macAddress } from 'macaddress';

async function foo() {
    try {
      return `MAC:${await macAddress()}`;
    } catch (error) {
      console.warn('Failed to get MAC address', error);
    }

    // return something else
    // ...
}

In our case we use the MAC address as a machine identifier but this means we cannot identify the machine when it is offline.

Why not support Promise?

Promise is very useful for high readability code. why not support?
If possible, i want to submit a pull request for this issue

Please release version 0.2.9

Hey there,

your package is used in uniqid which I use in my project. Unfortunately my project is stuck with node 4 (thanks aws) and I need to use the --use_strict flag to start my application.

The version 0.2.8 of your package contains an error which is fixed in the current master: https://github.com/scravy/node-macaddress/blob/0.2.8/index.js#L34

This only fails when strict mode is turned on.

Would be super awesome if you could just release 0.2.9. This would solve my issue immediately.

linux implementation can read arbitrary files

execFile("cat", ["/sys/class/net/" + iface + "/address"], function (err, out) {

By prepending ../../.. to the "interface" name, this line of code can be asked read a file from anywhere on the filesystem as long as that file is named address.

It is also a little bit strange to run cat to read a file rather than just using the node fs module, but patching only that will not solve the security problem.

Not working in Ubuntu Bash on Windows

Using it on Windows Subsystem for Linux gives this error:

> macaddress.one(function (err, mac) {console.log("Mac address for this host: %s", mac);});
Error: EINVAL: invalid argument, uv_interface_addresses
    at Error (native)
    at Object.lib.networkInterfaces (/mnt/c/.../node_modules/macaddress/index.js:31:21)
    at Object.lib.one (/mnt/c/.../node_modules/macaddress/index.js:79:26)

Does not return any MAC addresses when offline

I've been using this module in a project, and users have reported problems on various platforms -- the problems all boil down to my use of macaddress, but only when their machines are offline.

I haven't been able to reproduce the issue personally on OSX, but on Windows 8.1 I see the problem.

mac.all(function (err, all) {
    for (var prop in all) {
        console.log(all[ prop ].mac); //will always be blank output when offline
    }
});

To be clear, my Windows 8.1 machine does not have WIFI -- it only uses a hard ethernet connection. Removing that hard line is when I see the issue.

Linux: cat is not in path

I am running my nodejs app as systemd service and there is no PATH var set for 'cat'

had to change lib/linux.js

execFile("cat...
to
execFile("/bin/cat...

Throws on node 18

There is a breaking change in node 18
family property is now number not a string

nodejs/node#42787

I'd throw up a PR but I'm not sure how to treat number.
Weather it is ok just to .toString() it or "IPv" + address.family

It will be great if getAllInterfaces method is public.

os.networkInterfaces() returns only informations about interfaces that have IP set.
lib.one supports to get the mac address of an interface that doesn't have the IP set, because is using internally getAllInterfaces.

My problem is that I need the mac address of an adapter that is not returned by lib.networkInterfaces, because is using internally os.networkInterfaces().

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.