Implementation code:
Copy Code code as follows:
var OS = require (' OS ');
function Getlocalip () {
var map = [];
var ifaces = os.networkinterfaces ();
Console.log (ifaces);
for (Var dev in ifaces) {
if (Dev.indexof (' eth0 ')!=-1) {
var tokens = dev.split (': ');
var dev2 = null;
if (tokens.length = = 2) {
Dev2 = ' eth1: ' + tokens[1];
else if (tokens.length = 1) {
Dev2 = ' eth1 ';
}
if (null = = Ifaces[dev2]) {
Continue
}
Find the IP of eth0 and eth1 respectively
var IP = null, IP2 = NULL;
Ifaces[dev].foreach (function (details) {
if (details.family = = ' IPv4 ') {
ip = details.address;
}
});
Ifaces[dev2].foreach (function (details) {
if (details.family = = ' IPv4 ') {
IP2 = details.address;
}
});
if (null = IP | | null = = IP2) {
Continue
}
//Add records to map
if (Ip.indexof (' 10. ') = = 0 | |
ip.indexof (' 172. ') = = 0 | |
ip.indexof (' 192. ') = = 0) {
Map.push ({" Intranet_ip ": IP," internet_ip ": Ip2});
} else {
Map.push ({"Intranet_ip": Ip2, "internet_ip": IP});
}
}
}
return map;
}
Console.log (Getlocalip ());