What is address?
The 'address' npm package is a utility for retrieving detailed information about the network interfaces of the host machine. It can be used to get IP addresses, MAC addresses, and DNS servers, among other things. This package is particularly useful for applications that need to be aware of the network configuration of the host they are running on.
What are address's main functionalities?
Get IP Address
This feature allows you to retrieve the IPv4 or IPv6 address of the host machine. It's useful for applications that need to know their own IP address, for instance, to communicate with other services in a network.
const address = require('address');
// Get the IPv4 address
address.ip(); // '192.168.0.2'
// Get the IPv6 address
address.ipv6(); // 'fe80::7ca0:ab22:703a:b329'
Get MAC Address
This feature enables you to get the MAC address of the host machine. This can be useful for applications that need to identify the network interface of the host.
const address = require('address');
// Get the MAC address
address.mac(function (err, addr) {
console.log(addr); // '78:ca:39:b0:e6:7c'
});
Get DNS Servers
This feature provides the DNS servers configured on the host machine. It's useful for applications that need to perform DNS lookups or need to know about the network's DNS configuration.
const address = require('address');
// Get DNS servers
console.log(address.dns()); // ['8.8.8.8', '8.8.4.4']
Other packages similar to address
ip
The 'ip' package is similar to 'address' in that it provides utilities for IP address manipulation and querying. However, it focuses more on the manipulation and calculation of IP addresses rather than retrieving network interface details.
network
The 'network' package offers functionality to get network information such as the user's IP, MAC address, and network interfaces, similar to 'address'. However, it also includes additional features like checking internet connectivity, which 'address' does not provide.
os
While not a third-party package, Node.js's built-in 'os' module provides some overlapping functionality with 'address', such as retrieving network interfaces. However, 'address' offers more detailed and specific network information retrieval capabilities.
address
Get current machine IP, MAC and DNS servers.
DNS servers receive from /etc/resolv.conf
.
Install
$ npm install address
Usage
Get IP is sync and get MAC is async for now.
var address = require('address');
address.ip();
address.ipv6();
address.mac(function (err, addr) {
console.log(addr);
});
address.ip('lo');
address.mac('vboxnet', function (err, addr) {
console.log(addr);
});
Get all addresses: IPv4, IPv6 and MAC
address(function (err, addrs) {
console.log(addrs.ip, addrs.ipv6, addrs.mac);
});
address('vboxnet', function (err, addrs) {
console.log(addrs.ip, addrs.ipv6, addrs.mac);
});
Get DNS servers
address.dns(function (err, addrs) {
console.log(addrs);
});
License
(The MIT License)
Copyright (c) 2013 fengmk2 <fengmk2@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.