How to get public ip and local ip address in Raspberry pi 3 using nodejs

Posted By : Chandan Kumar Singh | 28-Dec-2016

In this blog we will fetch Raspberry pi internal ip address and public ip address if it is connected to the internet.

For getting public ip address you can use external-ip library.

Example

 
 require('external-ip')()(function(err, ip) {
            if (!err) {
                console.log(ip);
            } else {
                console.log(err);
            }
        });

 

To get internal ip address.

 
 var interfaces = require('os').networkInterfaces();
        var addresses = [];
        for (var k in interfaces) {
            for (var k2 in interfaces[k]) {
                var address = interfaces[k][k2];
                if (address.family === 'IPv4' && address.address !== '127.0.0.1' && !address.internal){
                    addresses.push(address.address);
                }
            }
        }
        console.log(addresses);
 

Now you can further process the ips according to your requirement.

Thanks 

Related Tags

About Author

Author Image
Chandan Kumar Singh

Chandan is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate. He loves technologies like Blockchain and IoT.

Request for Proposal

Name is required

Comment is required

Sending message..