VOOZH about

URL: https://www.geeksforgeeks.org/node-js/node-js-os-networkinterfaces-method/

⇱ Node.js os.networkInterfaces() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Node.js os.networkInterfaces() Method

Last Updated : 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: 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: 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
Comment

Explore