什么';s curl和nslookup之间的名称查找差异

什么';s curl和nslookup之间的名称查找差异,curl,dns,nslookup,dig,Curl,Dns,Nslookup,Dig,当我试图计算DNS查询何时超时时,我不知所措。尝试了多种方案(在Linux上): 在/etc/resolv.conf中未配置名称服务器 ###################### curl ####################### WRITE_OUT="%{http_code}\t%{time_namelookup}\t%{time_connect}\t\t%{time_starttransfer}\t\t%{time_total}\n" time curl -k -w "$WRITE

当我试图计算DNS查询何时超时时,我不知所措。尝试了多种方案(在Linux上):

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

    ###################### curl #######################
    WRITE_OUT="%{http_code}\t%{time_namelookup}\t%{time_connect}\t\t%{time_starttransfer}\t\t%{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需要更长的时间(24s)。这让我非常困惑,curl的行为更有意义,因为主机上没有指定名称服务器

  • 在/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
    
    万岁!看起来像curl和nslookup在同一页上

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

    ###################### 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
    
    又糊涂了

  • 最令人困惑的是,从的手册页中,我们可以看到
    超时
    的默认值为5秒,
    尝试次数
    为2次。所以我想超时应该是5秒*2=10秒。但是…令人困惑

    编辑: 再次尝试修改
    /etc/nsswitch.conf
    ,仅使用
    dns
    方法<代码>主机: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
    
    ##################################################
    时间旋度-k-w“$WRITE_OUT”https://www.google.com/
    000     0.000   0.000           0.000           0.000
    curl:(6)无法解析主机:www.google.com;未知错误
    实0.051s
    用户0m0.004s
    系统0m0.002s
    #####################nslookup####################
    时间:www.google.com
    ;; 连接超时;尝试下一个来源
    ;; 连接超时;无法访问任何服务器
    真实0m24.287s
    用户0.005s
    系统0m0.014s
    ########################挖掘######################
    时间挖掘www.google.com
    ;  DiG 9.9.4-RedHat-9.9.4-51.el7 www.google.com
    ;; 全局选项:+cmd
    ;; 连接超时;无法访问任何服务器
    实数0m18.041s
    用户0.005s
    系统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
    
    time curl-k-w“$WRITE\u OUT”https://www.google.com/
    000     0.000   0.000           0.000           19.527
    curl:(6)无法解析主机:www.google.com;未知错误
    真实0m20.533s
    用户0m0.003s
    系统0m0.004s
    时间:www.google.com
    ;; 连接超时;尝试下一个来源
    ;; 连接超时;无法访问任何服务器
    实际0m20.009s
    用户0.005s
    系统0m0.005s
    时间挖掘www.google.com
    ;  DiG 9.9.4-RedHat-9.9.4-51.el7 www.google.com
    ;; 全局选项:+cmd
    ;; 连接超时;无法访问任何服务器
    实数0m15.008s
    用户0.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
    
    time curl-k-w“$WRITE\u OUT”https://www.google.com/
    000     0.000   0.000           0.000           4.512
    curl:(6)无法解析主机:www.google.com;未知错误
    实际0m5.518s
    用户0m0.004s
    sys 0m0.003s
    时间:www.google.com
    ;; 连接超时;尝试下一个来源
    ;; 连接超时;无法访问任何服务器
    实际0m20.009s
    用户0.005s
    系统0m0.005s
    时间挖掘www.google.com
    ;  DiG 9.9.4-RedHat-9.9.4-51.el7 www.google.com
    ;; 全局选项:+cmd
    ;; 连接超时;无法访问任何服务器
    real 0m15.009s
    用户0.005s
    系统0m0.005s
    
    dig
    有自己的超时机制,超时(5s)*重试(3)=15s。

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


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

    虽然这是一篇老文章,但我想插一句,因为它已经为我出现了不止一次,所以我想分享它

    需要指出的一个区别是应用程序(即nslookup或curl)用于DNS查找的内容,即
    libresolv.so
    libbind.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输出应该显示每个部分等待的时间以及底层系统调用正在执行的工作。

    dig
    确实应该优先考虑,但将显示与此处一致的行为,此处相同的行为,我只是认为我应该提及它。老实说,我甚至不知道为什么nslookup会被弃用。
    dig
    是一个超集,有更多的功能,以及更明智的默认行为。请参阅示例以获取解释。由于my/etc/hosts不包含“www.google.com”条目,因此nslookup所需的时间~=curl所需的时间,对吗?如果您只希望简洁的输出,而不需要所有详细信息,可以使用flag
    +short
    。但是它将只显示
    应答
    部分中的内容,而不显示其他内容,因此有时如果没有该标志,它就不那么容易混淆。还要记住,DNS不是名称->IP映射的唯一来源。您将在
    /etc/nsswitch.conf
    中找到您的系统正在使用的源以及使用的顺序。我只找到了一个与dns相关的条目,“hosts:files dns myhostname”。不确定最后一个
    myhostname
    是什么意思。看起来我可以将其更改为主机:dns,然后再试一次,samensswitch.conf的结果会影响命令
    curl
    ,但不会影响
    nslookup
    dig
    ,看起来
    nslookup
    dig
    直接使用/etc/resolv.conf。是的,由于这两个工具只是DNS工具,但任何其他根据名称向操作系统请求IP的应用程序都将调用相应的
    libc
    函数,如
    getaddrinfo
    ,然后在内部使用
    nsswitch.conf