用Go net 工具包获取DNS 信息
🕥 Mon, 26 Nov 2018 by GolangNote
Golang 内置库net 有一些函数能方便获取DNS 记录信息。
LookupIP
通过域名查询 IPv4 和 IPv6 信息
package main
import (
"fmt"
"net"
)
func main() {
ipRecords, _ := net.LookupIP("google.com")
for _, ip := range ipRecords {
fmt.Println(ip)
}
}
输出:
216.58.197.110
2404:6800:4005:804::200e
LookupCNAME
获取CNAME 信息
package main
import (
"fmt"
"net"
)
func main() {
cname, _ := net.LookupCNAME("m.baidu.com")
fmt.Println(cname)
}
输出:
wap.n.shifen.com.
LookupNS
Name Server (NS)
package main
import (
"fmt"
"net"
)
func main() {
nameServer, _ := net.LookupNS("baidu.com")
for _, ns := range nameServer {
fmt.Println(ns)
}
}
输出:
&{ns4.baidu.com.}
&{ns2.baidu.com.}
&{ns7.baidu.com.}
&{dns.baidu.com.}
&{ns3.baidu.com.}
MX records
package main
import (
"fmt"
"net"
)
func main() {
mxRecords, _ := net.LookupMX("baidu.com")
for _, mx := range mxRecords {
fmt.Println(mx.Host, mx.Pref)
}
}
输出:
mx.maillb.baidu.com. 10
mx.n.shifen.com. 15
mx50.baidu.com. 20
mx1.baidu.com. 20
jpmx.baidu.com. 20
SRV service record
package main
import (
"fmt"
"net"
)
func main() {
cname, srvs, err := net.LookupSRV("xmpp-server", "tcp", "baidu.com")
if err != nil {
panic(err)
}
fmt.Printf("\ncname: %s \n\n", cname)
for _, srv := range srvs {
fmt.Printf("%v:%v:%d:%d\n", srv.Target, srv.Port, srv.Priority, srv.Weight)
}
}
输出:
cname: _xmpp-server._tcp.baidu.com.
xmpp.wshifen.com.:5269:0:0
TXT records
package main
import (
"fmt"
"net"
)
func main() {
txtRecords, _ := net.LookupTXT("baidu.com")
for _, txt := range txtRecords {
fmt.Println(txt)
}
}
输出:
google-site-verification=GHb98-6msqyx_qqjGl5eRatD3QTHyVB6-xQ3gJB5UwM
v=spf1 include:spf1.baidu.com include:spf2.baidu.com include:spf3.baidu.com a mx ptr -all
利用第三方工具
上面是使用标准库net 得到DNS 信息,如果想要得到更多数据或更方便,可以使用第三方工具,如 https://github.com/miekg/dns 97
本文网址: https://golangnote.com/topic/245.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- Golang 把cookie 字符串解析为cookie 结构
- Golang 计算字符串中包含某个或某些字符集的个数
- 使用Golang 对文件增删写读操作备忘
- Go Modules 使用备忘
- 使用Golang 简单删除图片exif 信息
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
Top Go Articles
- Caddy、 SSLDocker、Nginx 性能比较及使用体验
- Golang byte to string 的方法实践
- Golang Json/map 合并/update
- golang 获取程序本身的md5值
- Golang Leveldb 基本使用的例子
- GoLang 获取变量类型
- golang 当前时间、时间戳、时区的相互转化
- golang map 按value 大小排序,降序和升序
- golang regexp 正则表达式常见的用法
- golang 正确获取绝对路径的方法
- Golang 定时循环的实现
- golang 把上传文件转为byte
- Golang 时区时差处理方式
- golang 用正则包regexp 通过user-agent 识别手机浏览器
- GoLang 正则判断字符是不是中文
- golang 生成良好的唯一ID/uuid库比较
- Golang 把cookie 字符串解析为cookie 结构
- Golang 获取两个数之间的随机数
- golang 用gzip 压缩、解压缩字符串
- GoLang 计算小文件和大文件 md5 值的例子