• Node.js Video Tutorials

Node.js - os.networkInterfaces() Method



The os module of Node.js provides a bunch of operating system-related utility methods and properties.

The Node.js os.networkInterfaces() method will return an object which contains the network interfaces that have been assigned a network address. In the returned object, each key specifies the network interface.

The returned object contains network interfaces with some properties. These properties are described below −

  • address − This is a string that has the assigned IPV4 or IPV6 address.

  • netmask − This is a string that has the assigned IPV4 or IPV6 network mask.

  • netmask − This string can be either IPV4 or IPV6.

  • mac − A string that shows the mac address of the network interface

  • internal − This is a Boolean value that will be true if the network interface is a loopback or similar interface that is not remotely accessible. Else, it is false.

  • scopeid − This is a number that has the IPV6 scope ID and is applicable only when the family is IPV6.

cidr − This is a string that specifies the assigned IPV4 or IPV6 address with the routing prefix in CIDR notation. The property is set to NULL if the netmask is invalid.

Syntax

Following is the syntax of the Node.js os.networkInterfaces() method −

os.networkInterfaces()

Parameters

This method does not accept any parameters.

Return value

This method returns an object containing information about network interfaces of a particular network.

Example

In the following example, we are trying to print the network interfaces of a network by using the os.networkInterfaces() method.

const os = require('os');
console.log("The network interfaces which have been assigned to this Network address are: ") 
console.log(os.networkInterfaces());

Output

As we can see in the output below, the current computer is connected to an Ethernet network, and the os.networkInterfaces() method returned the network interfaces of the connected 'Ethernet' network.

The network interfaces which have been assigned to this Network address are:
{
  Ethernet: [
      {
         address: 'fe80::46d1:d65a:b6cc:2926',
         netmask: 'ffff:ffff:ffff:ffff::',
         family: 'IPv6',
         mac: '88:a4:c2:bb:d1:f9',
         internal: false,
         cidr: 'fe80::46d1:d65a:b6cc:2926/64',
         scopeid: 8
      },
      {
         address: '192.168.2.33',
         netmask: '255.255.255.0',
         family: 'IPv4',
         mac: '88:a4:c2:bb:d1:f9',
         internal: false,
         cidr: '192.168.2.33/24'
      }
   ],
  'Loopback Pseudo-Interface 1': [
      {
         address: '::1',
         netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
         family: 'IPv6',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '::1/128',
         scopeid: 0
      },
      {
         address: '127.0.0.1',
         netmask: '255.0.0.0',
         family: 'IPv4',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '127.0.0.1/8'
      }
   ]
}

Example

In the following example, we are trying to print the details of various network interfaces to which the current system is connected.

const os = require('os');
console.log(os.networkInterfaces());

Output

After executing the above program, the os.networkInterfaces() method returned the network interfaces of the connected 'Wi-Fi' network.

{
   'Wi-Fi': [
      {
         address: 'fe80::57e9:cc91:95fc:5f5b',
         netmask: 'ffff:ffff:ffff:ffff::',
         family: 'IPv6',
         mac: 'bc:f1:71:3b:eb:a1',
         internal: false,
         cidr: 'fe80::57e9:cc91:95fc:5f5b/64',
         scopeid: 16
      },
      {
         address: '192.168.25.229',
         netmask: '255.255.255.0',
         family: 'IPv4',
         mac: 'bc:f1:71:3b:eb:a1',
         internal: false,
         cidr: '192.168.25.229/24'
      }
   ],
   'Loopback Pseudo-Interface 1': [
      {
         address: '::1',
         netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
         family: 'IPv6',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '::1/128',
         scopeid: 0
      },
      {
         address: '127.0.0.1',
         netmask: '255.0.0.0',
         family: 'IPv4',
         mac: '00:00:00:00:00:00',
         internal: true,
         cidr: '127.0.0.1/8'
      }
   ]
}
nodejs_os_module.htm
Advertisements