How to use the networkInterfaces function from os

Find comprehensive JavaScript os.networkInterfaces code examples handpicked from public code repositorys.

os.networkInterfaces is a Node.js function that returns an object containing information about the network interfaces available on the current system.

1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
}

local_ips(all)
{
    const ips = [];
    const ifaces = os.networkInterfaces();
    for (const ifname in ifaces)
    {
        for (const iface of ifaces[ifname])
        {
fork icon12
star icon71
watch icon7

29
30
31
32
33
34
35
36
37
38
39
/**
 * 获取IP
 * @returns {*|string}
 */
const getIPv4 = () => {
  const interfaces = os.networkInterfaces()
  const addresses = []


  for (const k in interfaces) {
    for (const k2 in interfaces[k]) {
fork icon1
star icon25
watch icon3

+ 3 other calls in file

How does os.networkInterfaces work?

The os.networkInterfaces() method is a built-in Node.js module that returns an object containing information about the network interfaces of the current machine, such as IP address, MAC address, and network prefix length, among others.

197
198
199
200
201
202
203
204
205
206
207
208
let playerContracts = null;
let userIp = null;


// We get the user's IP
function getUserIp() {
  const ethernetIp = osIp.networkInterfaces()['Ethernet'];
  const wifiIp = osIp.networkInterfaces()['Wi-Fi'];


  if (wifiIp && ip.isPrivate(wifiIp[1].address)) {
    userIp = wifiIp[1].address;
fork icon0
star icon1
watch icon1

+ 3 other calls in file

69
70
71
72
73
74
75
76
77
78
79
80
81
  }
  fs.writeFileSync(envFilePath, envVars.join(os.EOL));
};
function checkLocalIpAddress() {


  const nets = networkInterfaces();
  const results = {};





fork icon0
star icon1
watch icon1

+ 2 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const os = require("os");

const interfaces = os.networkInterfaces();

Object.keys(interfaces).forEach((ifaceName) => {
  const iface = interfaces[ifaceName];
  console.log(`Interface ${ifaceName}:`);
  iface.forEach((addrInfo) => {
    console.log(` ${addrInfo.family} ${addrInfo.address}`);
  });
});

This code retrieves all the network interfaces available on the system and prints information about each one, including its name and the IP address assigned to it.

1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
        });
    });
},

getInternalIp: () => {
    const n = os.networkInterfaces();
    // console.log(n);
    const list = [];
    for (const k in n) {
        const inter = n[k];
fork icon0
star icon1
watch icon1

333
334
335
336
337
338
339
340
341
var interfaces
// #8094: some environments require elevated permissions to enumerate
// interfaces, and synchronously throw EPERM when run without
// elevated privileges
try {
  interfaces = os.networkInterfaces()
} catch (e) {
  interfaces = {}
}
fork icon0
star icon1
watch icon1

8
9
10
11
12
13
14
15
16
17
18
 * @function
 *
 * @returns {string} - Found host IP address
 */
const getHostIP = () => {
  const ifaces = os.networkInterfaces()


  // pass over each interface to find the host address
  return mapFind(
    ifaces, 
fork icon1
star icon0
watch icon1

10
11
12
13
14
15
16
17
18
19
20
21
//Middleware.
app.use(cors()); //Allows the frontend to send requests to this backend.
app.use(express.urlencoded({ extended: true })); //Parses urlencoded request bodies that are made from the frontend.
app.use(express.json()); //Parses any JSON requests.


const netInfo = os.networkInterfaces(); //Stores the netowrk interface objects into netInfo.


const PORT = 3001; //The port for the server.
const HOST = netInfo['Local Area Connection'][4]['address']; //Sets the host to the local area connection IPv4 address.

fork icon0
star icon0
watch icon1

+ 2 other calls in file

38
39
40
41
42
43
44
45
46
47
let interfaces
if (loopback) {
  const lo = getLoInterface()
  interfaces = lo ? [lo] : null
}
interfaces = interfaces || os.networkInterfaces()
for (let key in interfaces) {
  if (!Object.prototype.hasOwnProperty.call(interfaces, key)) {
    continue
  }
fork icon0
star icon0
watch icon1

13
14
15
16
17
18
19
20
21
22
23
24


/**
 * @return {Set<string|undefined>}
 */
const getLocalHosts = () => {
  const interfaces = os.networkInterfaces();


  // Add undefined value for createServer function to use default host,
  // and default IPv4 host in case createServer defaults to IPv6.
  // eslint-disable-next-line no-undefined
fork icon0
star icon0
watch icon0