Open In App

Node.js os.networkInterfaces() Method

Last Updated : 12 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

The os.networkInterfaces() method is an inbuilt application programming interface of the os module which is used to get the information about network interfaces of the computer.

Syntax:

os.networkInterfaces()

Parameters: This method does not accept any parameters.

Return Value: This method returns an object containing information about each network interfaces. The returned object will contain the array of network interfaces which again consists of the following attributes:

  • address: A string that specifies the assigned network address i.e. IPv4 or IPv6/
  • netmask: A string that specifies the IPv4 or IPv6 network mask.
  • family: A string that specifies the Family, value is one of IPv4 or IPv6.
  • mac: A string that specifies the MAC address of the network interface.
  • internal: A boolean value, true if interface is loopback one, false otherwise.
  • scopeid: A number that specifies the scope ID for IPv6.
  • cidr: A string that specifies the assigned IPv4 or IPv6 address with the routing prefix in CIDR notation. It is set to null if netmask is invalid.

Below examples illustrate the use of os.networkInterfaces() method in Node.js:

Example 1:




// Node.js program to demonstrate the   
// os.networkInterfaces() Method
  
// Allocating os module
const os = require('os');
  
// Print os.networkInterfaces() value
console.log(os.networkInterfaces());


Output:

{ 'Wi-Fi':
   [ { address: 'fe80::242a:3451:7fb2:3ab1',
       netmask: 'ffff:ffff:ffff:ffff::',
       family: 'IPv6',
       mac: 'b3:52:16:13:de:b9',
       scopeid: 3,
       internal: false,
       cidr: 'fe80::242a:3451:7fb2:3ab1/64' },
     { address: '192.168.0.5',
       netmask: '255.255.255.0',
       family: 'IPv4',
       mac: 'b3:52:16:13:de:b9',
       internal: false,
       cidr: '192.168.0.5/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',
       scopeid: 0,
       internal: true,
       cidr: '::1/128' },
     { 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 2:




// Node.js program to demonstrate the   
// os.networkInterfaces() Method
  
// Allocating os module
const os = require('os');
  
// Print os.networkInterfaces() value
var net_int = os.networkInterfaces();
  
var no_of_network_interfaces = 0;
  
for (var key in net_int) {
  console.log(key);
  var net_infos=net_int[key];
     
  net_infos.forEach(element => {      
  no_of_network_interfaces++;
  console.log("\tinterface:");
  
    for (var attr in element){
      console.log("\t\t" + attr + 
          " : " + element[attr] );
    }
  });  
}
  
console.log("total number of Network "
  + "interfaces is " + no_of_network_interfaces);


Output:

Wi-Fi
        interface:
                address : fe80::242a:3451:7fb2:3ab1
                netmask : ffff:ffff:ffff:ffff::
                family : IPv6
                mac : b3:52:16:13:de:b9
                scopeid : 3
                internal : false
                cidr : fe80::242a:3451:7fb2:3ab1/64
        interface:
                address : 192.168.0.5
                netmask : 255.255.255.0
                family : IPv4
                mac : b3:52:16:13:de:b9
                internal : false
                cidr : 192.168.0.5/24
Loopback Pseudo-Interface 1
        interface:
                address : ::1
                netmask : ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff
                family : IPv6
                mac : 00:00:00:00:00:00
                scopeid : 0
                internal : true
                cidr : ::1/128
        interface:
                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
total number of Network interfaces is 4

Note: The above program will compile and run by using the node filename.js command.

Reference: https://nodejs.org/api/os.html#os_os_networkinterfaces



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads