Open In App

Node.js os.networkInterfaces() Method

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:



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


Article Tags :