+ 1

Node js question

I'm trying to access the data from os.networkInterfaces(). I can display it but I can't figure out how to access the individual attributes. I looks like an array of objects? Maybe?

19th Jun 2020, 8:41 AM
Garrowolf Sauber
Garrowolf Sauber - avatar
10 Answers
+ 1
Garrowolf Sauber I do not wish to make the derived work complicated, but I can't see there's a direct way, we have to get all array of adapter list before can iterate through all the settings of each derived adapters.
19th Jun 2020, 10:08 AM
Calviղ
Calviղ - avatar
+ 2
const os = require('os'); const ni = os.networkInterfaces(); console.log(ni); // this would print all the network adpater settings The output are the objects of all the network adapters name. Each network adapter object has settings in array. Each setting array has consists of object: address, netmask, family, mac, internal and cidr.
19th Jun 2020, 8:51 AM
Calviղ
Calviղ - avatar
+ 2
To list the adapters const os = require('os'); const ni = os.networkInterfaces(); console.log(ni); // this would print all the network adpater settings const os = require('os'); const ni = os.networkInterfaces(); const adapters = Object.keys(ni); console.log(adapters.join(',')); // this prints all the adapter names
19th Jun 2020, 9:02 AM
Calviղ
Calviղ - avatar
+ 1
How do I access each of those attributes? Say I need to reference the mac address, how do I do that?
19th Jun 2020, 9:00 AM
Garrowolf Sauber
Garrowolf Sauber - avatar
+ 1
How would I get to the attributes of a specific adapter?
19th Jun 2020, 9:06 AM
Garrowolf Sauber
Garrowolf Sauber - avatar
+ 1
If you want to get attribute of a specific adapter for instance: to get all mac addresses of first adapter, adapters[0] const os = require('os'); const ni = os.networkInterfaces(); // console.log(ni); // this would print all the network adpater settings const adapters = Object.keys(ni); // console.log(adapters.join(',')); const macAdapter1 = ni[adapters[0]].map( ap => { return ap.mac; }); console.log(macAdapter1.join(',')); // this prints all mac addresses of adapter[0]
19th Jun 2020, 9:19 AM
Calviղ
Calviղ - avatar
+ 1
thank you! So I don't understand what is going on with this line: const macAdapter1 = ni[adapters[0]].map( ap => { return ap.mac; });
19th Jun 2020, 9:32 AM
Garrowolf Sauber
Garrowolf Sauber - avatar
+ 1
Cool beans! thank you!
19th Jun 2020, 9:43 AM
Garrowolf Sauber
Garrowolf Sauber - avatar
0
Is there a way of doing this with object notation? say you want the mac address of the first element.
19th Jun 2020, 9:50 AM
Garrowolf Sauber
Garrowolf Sauber - avatar