如何将主机名解析为节点js中的IP地址 [英] How to resolve hostname to an ip address in node js

查看:141
本文介绍了如何将主机名解析为节点js中的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将hosts文件中定义的主机名解析为其对应的IP地址.

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

例如,我的主机文件如下所示-"/etc/hosts"

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

现在在我的node.js中,我可以读取此文件的内容,但是我需要为给定的hostname提取.

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或任何第三方软件包都无法安装在计算机上.

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

非常感谢您的帮助!

推荐答案

NodeJS文档-DNS –您检查了吗?

const dns = require('dns')

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

这篇关于如何将主机名解析为节点js中的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆