os.hostname() to return an IP

588 views Asked by At

I have this raspberryPi with Node.JS running on top of it. By calling os.hostname(), the Pi returns "raspberry". I'm aware on how to modify this behaviour. What I don't know is how to do so in order to have it return an IP.

The raspberry is connected in a distributed application in which I use remote procedure call to execute functions. To be reachable, the Pi needs to have an address, but I'm not entirely sure on how to do this.

Thanks!

1

There are 1 answers

1
Ignotus On BEST ANSWER

Try to return the first IP address for eth0

require('os').networkInterfaces().eth0[0].address;

or

os.networkInterfaces().eth0[0].address;