UNPKG

5.2 kBMarkdownView Raw
1# address
2
3[![NPM version][npm-image]][npm-url]
4[![Node.js CI](https://github.com/node-modules/address/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/address/actions/workflows/nodejs.yml)
5[![Test coverage][coveralls-image]][coveralls-url]
6[![npm download][download-image]][download-url]
7
8[npm-image]: https://img.shields.io/npm/v/address.svg?style=flat-square
9[npm-url]: https://npmjs.org/package/address
10[coveralls-image]: https://img.shields.io/coveralls/node-modules/address.svg?style=flat-square
11[coveralls-url]: https://coveralls.io/r/node-modules/address?branch=master
12[download-image]: https://img.shields.io/npm/dm/address.svg?style=flat-square
13[download-url]: https://npmjs.org/package/address
14
15Get current machine IPv4, IPv6, MAC and DNS servers.
16
17DNS servers receive from `/etc/resolv.conf`.
18
19## Install
20
21```bash
22npm install address
23```
24
25## Usage
26
27Get IP is sync and get MAC is async for now.
28
29- esm & typescript
30
31```ts
32import { ip, ipv6, mac } from 'address';
33
34// default interface 'eth' on linux, 'en' on osx.
35ip(); // '192.168.0.2'
36ipv6(); // 'fe80::7aca:39ff:feb0:e67d'
37mac(function (err, addr) {
38 console.log(addr); // '78:ca:39:b0:e6:7d'
39});
40
41// local loopback
42ip('lo'); // '127.0.0.1'
43
44// vboxnet MAC
45mac('vboxnet', function (err, addr) {
46 console.log(addr); // '0a:00:27:00:00:00'
47});
48```
49
50- commonjs
51
52```js
53const { ip, ipv6, mac } = require('address');
54
55// default interface 'eth' on linux, 'en' on osx.
56ip(); // '192.168.0.2'
57ipv6(); // 'fe80::7aca:39ff:feb0:e67d'
58mac(function (err, addr) {
59 console.log(addr); // '78:ca:39:b0:e6:7d'
60});
61
62// local loopback
63ip('lo'); // '127.0.0.1'
64
65// vboxnet MAC
66mac('vboxnet', function (err, addr) {
67 console.log(addr); // '0a:00:27:00:00:00'
68});
69```
70
71### Get all addresses: IPv4, IPv6 and MAC
72
73- esm & typescript
74
75```ts
76import { address } from 'address';
77
78address((err, addrs) => {
79 console.log(addrs.ip, addrs.ipv6, addrs.mac);
80 // '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d'
81});
82
83address('vboxnet', (err, addrs) => {
84 console.log(addrs.ip, addrs.ipv6, addrs.mac);
85 // '192.168.56.1', null, '0a:00:27:00:00:00'
86});
87```
88
89- commonjs
90
91```js
92const { address } = require('address');
93
94address((err, addrs) => {
95 console.log(addrs.ip, addrs.ipv6, addrs.mac);
96 // '192.168.0.2', 'fe80::7aca:39ff:feb0:e67d', '78:ca:39:b0:e6:7d'
97});
98
99address('vboxnet', (err, addrs) => {
100 console.log(addrs.ip, addrs.ipv6, addrs.mac);
101 // '192.168.56.1', null, '0a:00:27:00:00:00'
102});
103```
104
105### Get an interface info with family
106
107- esm & typescript
108
109```ts
110import { getInterfaceAddress } from 'address';
111
112getInterfaceAddress('IPv4', 'eth1');
113// { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' }
114```
115
116- commonjs
117
118```js
119const { getInterfaceAddress } = require('address');
120
121getInterfaceAddress('IPv4', 'eth1');
122// { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' }
123```
124
125### Get DNS servers
126
127- esm & typescript
128
129```js
130import { dns } from 'address';
131
132dns((err, servers) => {
133 console.log(servers);
134 // ['10.13.2.1', '10.13.2.6']
135});
136```
137
138- commonjs
139
140```js
141const { dns } = require('address');
142
143dns((err, servers) => {
144 console.log(servers);
145 // ['10.13.2.1', '10.13.2.6']
146});
147```
148
149### Promise style apis
150
151```ts
152import { address, mac, dns } from 'address/promises';
153
154const addr = await address();
155const macAddress = await mac();
156const servers = await dns();
157```
158
159## License
160
161[MIT](LICENSE.txt)
162
163<!-- GITCONTRIBUTOR_START -->
164
165## Contributors
166
167|[<img src="https://avatars.githubusercontent.com/u/156269?v=4" width="100px;"/><br/><sub><b>fengmk2</b></sub>](https://github.com/fengmk2)<br/>|[<img src="https://avatars.githubusercontent.com/u/1147375?v=4" width="100px;"/><br/><sub><b>alsotang</b></sub>](https://github.com/alsotang)<br/>|[<img src="https://avatars.githubusercontent.com/u/10237910?v=4" width="100px;"/><br/><sub><b>jkelleyrtp</b></sub>](https://github.com/jkelleyrtp)<br/>|[<img src="https://avatars.githubusercontent.com/u/63956?v=4" width="100px;"/><br/><sub><b>slyon</b></sub>](https://github.com/slyon)<br/>|[<img src="https://avatars.githubusercontent.com/u/1409643?v=4" width="100px;"/><br/><sub><b>mariodu</b></sub>](https://github.com/mariodu)<br/>|[<img src="https://avatars.githubusercontent.com/u/11351322?v=4" width="100px;"/><br/><sub><b>mathieutu</b></sub>](https://github.com/mathieutu)<br/>|
168| :---: | :---: | :---: | :---: | :---: | :---: |
169[<img src="https://avatars.githubusercontent.com/u/2139038?v=4" width="100px;"/><br/><sub><b>zhangyuheng</b></sub>](https://github.com/zhangyuheng)<br/>|[<img src="https://avatars.githubusercontent.com/u/32174276?v=4" width="100px;"/><br/><sub><b>semantic-release-bot</b></sub>](https://github.com/semantic-release-bot)<br/>|[<img src="https://avatars.githubusercontent.com/u/1400114?v=4" width="100px;"/><br/><sub><b>coolme200</b></sub>](https://github.com/coolme200)<br/>|[<img src="https://avatars.githubusercontent.com/u/5856440?v=4" width="100px;"/><br/><sub><b>whxaxes</b></sub>](https://github.com/whxaxes)<br/>
170
171This project follows the git-contributor [spec](https://github.com/xudafeng/git-contributor), auto updated at `Fri Sep 22 2023 20:49:32 GMT+0800`.
172
173<!-- GITCONTRIBUTOR_END -->