Node.js os.networkInterfaces() Method - GeeksforGeeks

12 Oct, 2021
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 moduleconst os = require('os');  // Print os.networkInterfaces() valueconsole.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 moduleconst os = require('os');  // Print os.networkInterfaces() valuevar 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
Node.js-os-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Express.js express.Router() Function
JWT Authentication with Node.js
Express.js req.params Property
Mongoose Populate() Method
Difference between npm i and npm ci in Node.js
Roadmap to Become a Web Developer in 2022
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to insert spaces/tabs in text using HTML/CSS?
Convert a string to an integer in JavaScript