首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >curl和nslookup之间的名称查找区别是什么

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

Stack Overflow用户
提问于 2018-01-19 11:28:01
回答 2查看 14.8K关注 0票数 3

在尝试确定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 -w "$WRITE_OUT”https://www.google.com/ 000 0.000 0.0000.000 0.000 curl:(6)无法解析主机: www.goole.com;未知错误real 0m0.009s用户0m0.000s sys 0m0.006s ##################### nslookup #################### time nslookup www.google.com ;;连接超时;正在尝试下一源;;连接超时;无法访问任何服务器real 0m24.012s用户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)无法解析主机: www.goole.com;未知错误real 0m20.535s用户0m0.003s sys 0m0.005s ##################### nslookup #################### time nslookup www.google.com ;;连接超时;正在尝试下一源;;连接超时;无法访问任何服务器real 0m20.008s用户0m0.006s sys 0m0.003s

万岁!看起来curl和nslookup在同一个页面上。

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

###################### curl ####################### time curl -k -w "$WRITE_OUT“https://www.google.com/ 000 0.000 0.000 0.000 4.513 curl:(6)无法解析主机: www.goole.com;未知错误real 0m5.520s用户0m0.004s sys 0m0.005s ##################### nslookup #################### time nslookup www.google.com ;;连接超时;正在尝试下一源;;连接超时;无法访问任何服务器real 0m20.010s用户0m0.006s sys 0m0.005s

又迷惑了!

最让人困惑的部分是,从resolv.conf的手册页面可以看出,timeout的默认值是5秒,attempts是2倍。所以我认为超时应该是5秒*2= 10秒。But...confusing...

编辑:重试修改/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。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48333686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档