导读 Dog是一个用于DNS查询的命令行工具,它的工作原理类似于dig。它具有丰富多彩的输出,支持DNS-over-TLS 和 DNS-over-HTTPS协议,并且可以输出JSON格式。
系统环境

Ubuntu 20.04.1 LTS

安装

Dog是用Rust编写的DNS查询工具。Dog需要rustc的版本高于1.45.0,我们使用apt安装的版本达不到这个要求,下面我们会使用其他方式安装rustc。下面需要先安装libssl-dev安装包:

bob@ubuntu-20-04:~$ sudo apt install -y cargo libssl-dev

下面安装rust,由于按照官方文档安装rust,有时下载没有进度,所以在这里先修改镜像源,改成国内的,然后执行安装:

bob@ubuntu-18-04:~$ export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
bob@ubuntu-18-04:~$ export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
bob@ubuntu-18-04:~$ curl https://sh.rustup.rs -sSf | sh


在下面的地方,选择选项1.


安装完成,需要执行下面命令,才能正常使用cargo命令:

bob@ubuntu-18-04:~$ source $HOME/.cargo/env

下面检查rustc的版本:

bob@ubuntu-18-04:~$ rustc --version
rustc 1.47.0 (18bf6b4f0 2020-10-07)


从github下载dog安装包,并解压:

bob@ubuntu-20-04:~$ wget https://github.com/ogham/dog/archive/v0.1.0.tar.gz
bob@ubuntu-20-04:~$ tar xvf v0.1.0.tar.gz 

进入解压出来的目录,并构建dog程序:

bob@ubuntu-20-04:~$ cd dog-0.1.0/


使用默认的仓库地址更新会比较慢,在这里按ctrl+c结束更新,在家目录会生成一个.cargo文件夹,进去新建一个config配置文件,将下面内容添加进去这样修改默认仓库,更新速度就快许多了:

bob@ubuntu-20-04:~/dog-0.1.0$ cat ~/.cargo/config

[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"

# 替换成你偏好的镜像源
replace-with = 'sjtu'
#replace-with = 'ustc'

# 清华大学
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

# 中国科学技术大学
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"

# 上海交通大学
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"

# rustcc社区
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"


修改完成之后,再回到dog-0.1.0目录里面,重新构建dog程序,并运行测试以下:

bob@ubuntu-18-04:~/dog-0.1.0$ cargo build
bob@ubuntu-18-04:~/dog-0.1.0$ cargo test


确保dog命令从任何目录运行,将二进制文件放在/usr/local/bin目录中。

bob@ubuntu-18-04:~/dog-0.1.0$ sudo cp -p ~/dog-0.1.0/target/debug/dog /usr/local/bin

运行dog工具,查看以下版本:

bob@ubuntu-18-04:~$ dog --version
dog ● command-line DNS client
v0.1.0 [] built on 2020-11-19 (pre-release!)
https://dns.lookup.dog/
bob@ubuntu-18-04:~$ 


可以使用源码包里面的命令补全脚本,当需要提示的时候按两下TAB键,显示子命令:

bob@ubuntu-18-04:~$ source ~/dog-0.1.0/completions/dog.bash 

如何使用dog命令

现在,让我们看看如何使用dog命令查询不同的DNS记录。

查询A记录
bob@ubuntu-18-04:~$ dog www.baidu.com
CNAME www.baidu.com.    5s   "www.a.shifen.com."
    A www.a.shifen.com. 4s   180.101.49.12
    A www.a.shifen.com. 4s   180.101.49.11

查询NS记录
bob@ubuntu-18-04:~$ dog aliyun.com NS
NS aliyun.com. 5s   "ns3.aliyun.com."
NS aliyun.com. 5s   "ns5.aliyun.com."
NS aliyun.com. 5s   "ns4.aliyun.com."

使用指定的域名服务器地址查询
bob@ubuntu-18-04:~$ dog google.com MX @1.1.1.1
MX google.com. 6m23s   30 "alt2.aspmx.l.google.com."
MX google.com. 6m23s   40 "alt3.aspmx.l.google.com."
MX google.com. 6m23s   10 "aspmx.l.google.com."
MX google.com. 6m23s   50 "alt4.aspmx.l.google.com."
MX google.com. 6m23s   20 "alt1.aspmx.l.google.com."

JSON格式输出
bob@ubuntu-18-04:~$ dog A www.baidu.com -J
{"responses":[{"additionals":[],"answers":[{"class":"IN","domain":"www.a.shifen.com.","name":"www.baidu.com.","ttl":5,"type":"CNAME"},{"address":"180.101.49.11","class":"IN","name":"www.a.shifen.com.","ttl":4,"type":"A"},{"address":"180.101.49.12","class":"IN","name":"www.a.shifen.com.","ttl":4,"type":"A"}],"authorities":[],"queries":[{"class":"IN","name":"www.baidu.com.","type":1}]}]}

查看帮助
bob@ubuntu-18-04:~$ dog --help

总结

Dog是一个用于DNS查询的命令行工具,它的工作原理类似于dig。它具有丰富多彩的输出,支持DNS-over-TLS 和 DNS-over-HTTPS协议,并且可以输出JSON格式。

本文原创地址:https://www.linuxprobe.com/dog-dns-query-tools.html编辑:逄增宝,审核员:逄增宝