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 |
|
15 | Get current machine IPv4, IPv6, MAC and DNS servers.
|
16 |
|
17 | DNS servers receive from `/etc/resolv.conf`.
|
18 |
|
19 | ## Install
|
20 |
|
21 | ```bash
|
22 | npm install address
|
23 | ```
|
24 |
|
25 | ## Usage
|
26 |
|
27 | Get IP is sync and get MAC is async for now.
|
28 |
|
29 | - esm & typescript
|
30 |
|
31 | ```ts
|
32 | import { ip, ipv6, mac } from 'address';
|
33 |
|
34 | // default interface 'eth' on linux, 'en' on osx.
|
35 | ip(); // '192.168.0.2'
|
36 | ipv6(); // 'fe80::7aca:39ff:feb0:e67d'
|
37 | mac(function (err, addr) {
|
38 | console.log(addr); // '78:ca:39:b0:e6:7d'
|
39 | });
|
40 |
|
41 | // local loopback
|
42 | ip('lo'); // '127.0.0.1'
|
43 |
|
44 | // vboxnet MAC
|
45 | mac('vboxnet', function (err, addr) {
|
46 | console.log(addr); // '0a:00:27:00:00:00'
|
47 | });
|
48 | ```
|
49 |
|
50 | - commonjs
|
51 |
|
52 | ```js
|
53 | const { ip, ipv6, mac } = require('address');
|
54 |
|
55 | // default interface 'eth' on linux, 'en' on osx.
|
56 | ip(); // '192.168.0.2'
|
57 | ipv6(); // 'fe80::7aca:39ff:feb0:e67d'
|
58 | mac(function (err, addr) {
|
59 | console.log(addr); // '78:ca:39:b0:e6:7d'
|
60 | });
|
61 |
|
62 | // local loopback
|
63 | ip('lo'); // '127.0.0.1'
|
64 |
|
65 | // vboxnet MAC
|
66 | mac('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
|
76 | import { address } from 'address';
|
77 |
|
78 | address((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 |
|
83 | address('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
|
92 | const { address } = require('address');
|
93 |
|
94 | address((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 |
|
99 | address('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
|
110 | import { getInterfaceAddress } from 'address';
|
111 |
|
112 | getInterfaceAddress('IPv4', 'eth1');
|
113 | // { address: '192.168.1.1', family: 'IPv4', mac: '78:ca:39:b0:e6:7d' }
|
114 | ```
|
115 |
|
116 | - commonjs
|
117 |
|
118 | ```js
|
119 | const { getInterfaceAddress } = require('address');
|
120 |
|
121 | getInterfaceAddress('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
|
130 | import { dns } from 'address';
|
131 |
|
132 | dns((err, servers) => {
|
133 | console.log(servers);
|
134 | // ['10.13.2.1', '10.13.2.6']
|
135 | });
|
136 | ```
|
137 |
|
138 | - commonjs
|
139 |
|
140 | ```js
|
141 | const { dns } = require('address');
|
142 |
|
143 | dns((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
|
152 | import { address, mac, dns } from 'address/promises';
|
153 |
|
154 | const addr = await address();
|
155 | const macAddress = await mac();
|
156 | const servers = await dns();
|
157 | ```
|
158 |
|
159 | ## License
|
160 |
|
161 | [MIT](LICENSE.txt)
|
162 |
|
163 |
|
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 |
|
171 | This 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 |
|