Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
169
rated 0 times [  173] [ 4]  / answers: 1 / hits: 38013  / 8 Years ago, mon, april 18, 2016, 12:00:00

I need to resolve hostname defined in hosts file to its corresponding IP address.



For example my host file look like this - /etc/hosts



127.0.0.1    ggns2dss81 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
192.168.253.8 abcdserver
192.168.253.20 testwsserver


Now in my node.js, i can read content of this file, but i need to fetch for given hostname.



hostname = testwsserver
hostIP = getIP(hostname);
console.log(hostIP); // This should print 192.168.253.20


PS - npm pkg or any third party package cannot be installed on machine.



Help is much appreciated!!


More From » node.js

 Answers
5

How about NodeJS documentation - DNS – have you checked it?



const dns = require('dns')

dns.lookup('testwsserver', function(err, result) {
console.log(result)
})

[#62505] Saturday, April 16, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
deving

Total Points: 26
Total Questions: 94
Total Answers: 103

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;