gpt4 book ai didi

node.js - Node.js 中是否有 nslookup 命令的等效功能?

转载 作者:太空宇宙 更新时间:2023-11-03 21:53:22 28 4
gpt4 key购买 nike

node.js 中是否有与 nslookup 等效的函数?

这是 nslookup 命令在我的 MacBook Pro 上的执行结果:

> nslookup www.amagicshop.com.tw 8.8.8.8
Server: 8.8.8.8
Address: 8.8.8.8#53

Non-authoritative answer:
www.amagicshop.com.tw canonical name = s16959.dname.91app.io.
s16959.dname.91app.io canonical name = proxy.letssl.91app.io.
proxy.letssl.91app.io canonical name = proxy-letssl-91app-io-196811564.ap-northeast-1.elb.amazonaws.com.
Name: proxy-letssl-91app-io-196811564.ap-northeast-1.elb.amazonaws.com
Address: 54.178.248.57
Name: proxy-letssl-91app-io-196811564.ap-northeast-1.elb.amazonaws.com
Address: 52.196.80.17

我想知道node.js中是否有一个函数,给定www.amagicshop.com.tw8.8.8.8作为输入,也会返回
s16959.dname.91app.io。proxy.letssl.91app.io.proxy-letssl-91app-io-196811564.ap-northeast-1.elb.amazonaws.com.52.196 .80.1754.178.248.57 作为输出。

我原本以为dns.resolveAny与 Node.js 中的 nslookup 等效函数。但我错了。

因为以下代码返回Error: queryAny ESERVFAIL www.amagicshop.com.tw错误。

const { Resolver } = require('dns')
const resolver = new Resolver()
resolver.setServers(['8.8.8.8'])

resolver.resolveAny('www.amagicshop.com.tw', (err, result) => {
if (err) {
console.error(`error: ${err}`)
} else {
console.log(`result: ${JSON.stringify(result)}`)
}
})

结果与nslookup的结果不同。

也许我必须实现一个结合了 resolveCnameresolve4 的函数,这样我才能实现我想要的。

最佳答案

为了获取所有规范名称,我们必须递归调用(在 nslookup 中,定义也是递归的!)。

CNAME

const dns = require('dns')

let accum = []
const getCnames = (err, result) => {
if (err) {
// no more records
console.log(accum)
return accum
} else {
const cname = result[0]
accum.push(cname)
return dns.resolveCname(cname, getCnames)
}
}

dns.resolveCname('www.amagicshop.com.tw', getCnames)

输出

[ 's16959.dname.91app.io',
'proxy.letssl.91app.io',
'proxy-letssl-91app-io-196811564.ap-northeast-1.elb.amazonaws.com' ]

IP地址

dns.resolve('www.amagicshop.com.tw', callback=(err, result) => {

if (err) {
console.error(`error: ${err}`)
} else {
console.log(result)
}
})

输出

[ '52.196.80.17', '54.178.248.57' ]

合并

const dns = require('dns')

const resolve = (cname) => {
const getIp = (accum) =>
dns.resolve(cname,
callback=(err, result) => {
if (err) {
console.error(`error: ${err}`)
} else {
result.push.apply(result, accum)
console.log(result)
}
})

let accum = []
const getCnames = (err, result) => {
if (err) {
// no more records
getIp(accum)
} else {
const cname = result[0]
accum.push(cname)
dns.resolveCname(cname, getCnames)
}
}

dns.resolveCname(cname, getCnames)
}

resolve('www.amagicshop.com.tw')

输出

[ '52.196.80.17',
'54.178.248.57',
's16959.dname.91app.io',
'proxy.letssl.91app.io',
'proxy-letssl-91app-io-196811564.ap-northeast-1.elb.amazonaws.com' ]

关于node.js - Node.js 中是否有 nslookup 命令的等效功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47544724/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com