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

时间:2018-01-19 03:28:01

标签: curl dns nslookup dig

在尝试确定DNS查询何时超时时,我迷路了。试过多个场景(在Linux上):

  1. /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的行为更有意义,因为主机上没有指定名称服务器。

  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
    

    乌拉!在同一页面上看起来像curl和nslookup。

  3. 添加可以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
    

    再次感到困惑!

  4. 最令人困惑的部分是,在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。

2 个答案:

答案 0 :(得分:1)

尽管这是一篇过时的文章,但我感觉很高兴,因为它对我来说不止一次出现,所以我想分享它。

需要指出的一个区别是应用程序(即nslookup或curl)用于DNS查找的内容,即<ROOT> <COSTRECOVERYSYSTEM_CONNECTION_STRING></COSTRECOVERYSYSTEM_CONNECTION_STRING> <COSTRECOVERYSYSTEM_EXTRACT_INTERVAL>60</COSTRECOVERYSYSTEM_EXTRACT_INTERVAL> <COSTRECOVERYSYSTEM_FILE_DATESTAMP>yyyyMMdd</COSTRECOVERYSYSTEM_FILE_DATESTAMP> <COSTRECOVERYSYSTEM_FILE_EXTENSION>txt</COSTRECOVERYSYSTEM_FILE_EXTENSION> <COSTRECOVERYSYSTEM_FILE_NAME>txt</COSTRECOVERYSYSTEM_FILE_NAME> <COSTRECOVERYSYSTEM_FILE_PATH>txt</COSTRECOVERYSYSTEM_FILE_PATH> </ROOT> libresolv.so。好像nslookup专门用于后者,所以也许这就是为什么它比curl更早超时的原因。要确定在您的系统上是否正常运行,您应该运行

libbind.so

并比较。

尽管是神秘的,但strace输出应显示每个部分等待多长时间以及底层系统调用正在执行的工作

答案 1 :(得分:0)

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

我已经读过nslookup已被弃用了。你有权访问 dig 吗?