Nodejs get the implementation code of Local intranet and extranet IP address _node.js

Source: Internet
Author: User

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 ());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.