+ 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?
10 Respuestas
+ 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.
+ 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.
+ 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
+ 2
Garrowolf Sauber you can study Array.map method here.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
+ 1
How do I access each of those attributes? Say I need to reference the mac address, how do I do that?
+ 1
How would I get to the attributes of a specific adapter?
+ 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]
+ 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;
});
+ 1
Cool beans! thank you!
0
Is there a way of doing this with object notation? say you want the mac address of the first element.