curl 和 nslookup 之间的名称查找有什么区别



在试图确定DNS查询何时超时时,我迷失了方向。尝试了多种场景(在Linux上):

  1. 在/etc/resolv.conf 中未配置名称服务器

    ###################### curl #######################
    WRITE_OUT="%{http_code}t%{time_namelookup}t%{time_connect}tt%{time_starttransfer}tt%{time_total}n"
    time curl -k -w "$WRITE_OUT" https://www.google.com/
    000     0.000   0.000           0.000           0.000
    curl: (6) Could not resolve host: www.goole.com; Unknown error
    real    0m0.009s
    user    0m0.000s
    sys     0m0.006s
    ##################### nslookup ####################
    time nslookup www.google.com
    ;; connection timed out; trying next origin
    ;; connection timed out; no servers could be reached
    real    0m24.012s
    user    0m0.004s
    sys     0m0.009s
    

    正如我们所看到的,curl 会立即返回(9ms),而 nslookup 需要更长的时间(24 秒)。这让我非常困惑,curl 的行为更有意义,因为主机上没有指定名称服务器。

  2. 在/etc/resolv.conf 中添加无法访问的主机 IP,无法 ping 以模拟名称服务器关闭情况

    ###################### curl #######################
    time curl -k -w "$WRITE_OUT" https://www.google.com/
    000     0.000   0.000           0.000           19.529  
    curl: (6) Could not resolve host: www.goole.com; Unknown error
    real    0m20.535s
    user    0m0.003s
    sys     0m0.005s
    ##################### nslookup ####################
    time nslookup www.google.com
    ;; connection timed out; trying next origin
    ;; connection timed out; no servers could be reached
    real    0m20.008s
    user    0m0.006s
    sys     0m0.003s
    

    万岁!看起来卷曲和nslookup在同一页面上。

  3. 添加一个可以 ping 但没有 DNS 服务的主机 IP 地址,以模拟服务器处于活动状态但名称服务器服务已关闭

    ###################### curl #######################
    time curl -k -w "$WRITE_OUT" https://www.google.com/
    000     0.000   0.000           0.000           4.513
    curl: (6) Could not resolve host: www.goole.com; Unknown error
    real    0m5.520s
    user    0m0.004s
    sys     0m0.005s
    ##################### nslookup ####################
    time nslookup www.google.com
    ;; connection timed out; trying next origin
    ;; connection timed out; no servers could be reached
    
    real    0m20.010s
    user    0m0.006s
    sys     0m0.005s
    

    又糊涂了!

最令人困惑的部分是,从 resolv.conf 的手册页面,我们可以知道timeout的默认值是 5 秒,attempts是 2 倍。所以我想超时应该是 5 秒 * 2 = 10 秒。但。。。混乱。。。

编辑: 再次尝试修改/etc/nsswitch.conf,仅使用dns方法。hosts: dns

场景 1:

###################### curl #######################
time curl -k -w "$WRITE_OUT" https://www.google.com/
000     0.000   0.000           0.000           0.000
curl: (6) Could not resolve host: www.google.com; Unknown error
real    0m0.051s
user    0m0.004s
sys     0m0.002s
##################### nslookup ####################
time nslookup www.google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
real    0m24.287s
user    0m0.005s
sys     0m0.014s
######################## dig ######################
time dig www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> www.google.com
;; global options: +cmd
;; connection timed out; no servers could be reached
real    0m18.041s
user    0m0.005s
sys     0m0.005s

场景 2:

time curl -k -w "$WRITE_OUT" https://www.google.com/
000     0.000   0.000           0.000           19.527
curl: (6) Could not resolve host: www.google.com; Unknown error
real    0m20.533s
user    0m0.003s
sys     0m0.004s
time nslookup www.google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
real    0m20.009s
user    0m0.005s
sys     0m0.005s
time dig www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> www.google.com
;; global options: +cmd
;; connection timed out; no servers could be reached
real    0m15.008s
user    0m0.005s
sys     0m0.003s

场景 3:

time curl -k -w "$WRITE_OUT" https://www.google.com/
000     0.000   0.000           0.000           4.512
curl: (6) Could not resolve host: www.google.com; Unknown error
real    0m5.518s
user    0m0.004s
sys     0m0.003s
time nslookup www.google.com
;; connection timed out; trying next origin
;; connection timed out; no servers could be reached
real    0m20.009s
user    0m0.005s
sys     0m0.005s
time dig www.google.com
; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7 <<>> www.google.com
;; global options: +cmd
;; connection timed out; no servers could be reached
real    0m15.009s
user    0m0.005s
sys     0m0.005s

dig有自己的超时机制,超时(5s)* 重试(3)= 15s。

虽然这是一篇旧帖子,但我想插话,因为它不止一次出现在我身上,所以我想分享它。

需要指出的一个区别是应用程序(即nslookup或curl)用于DNS查找的内容,即libresolv.solibbind.so。似乎 nslookup 专门做后者,所以也许这就是为什么它比 curl 更早超时的原因。要确定在您的系统上查看,您应该运行

strace -o curl.out curl www.google.com
strace -o dig.out dig www.google.com
grep libresolv *.out
grep libbind *.out

并进行比较。

虽然很神秘,但 Strace 输出应显示每个部分等待的时间以及底层系统调用正在执行工作

nslookup和类似的工具直接查询DNS,而curl首先检查本地/etc/hosts,然后查询DNS。因此,这可能是手头问题的线索。

我已经读到nslookup已被弃用。你有挖掘权吗?

相关内容

  • 没有找到相关文章

最新更新