cdh*_*hit 5 dns hostname wget ping
我用的时候wget
,主机名解析没问题
root:here cd$ wget https://gfe.cit.api.here.com/1/layer_put.json?layer_id=123&app_id=x2&app_code=x1
Run Code Online (Sandbox Code Playgroud)
结果是
Resolving gfe.cit.api.here.com... 52.51.134.116, 54.154.19.134, 52.208.9.155
Connecting to gfe.cit.api.here.com|52.51.134.116|:443... connected.
HTTP request sent, awaiting response... 400 Bad Request
2016-12-24 13:18:47 ERROR 400: Bad Request.
Run Code Online (Sandbox Code Playgroud)
但是当我使用 ping
ping https://gfe.cit.api.here.com/1/layer_put.json?layer_id=123&app_id=x2&app_code=x1
Run Code Online (Sandbox Code Playgroud)
结果是
cannot resolve https://gfe.cit.api.here.com/1/layer_put.json?layer_id=123: Unknown host
Run Code Online (Sandbox Code Playgroud)
主机名解析失败,有什么之间的区别wget
和ping
?
jll*_*gre 11
回复:
“ping”和“wget”在主机名解析方面有什么区别
Ping
需要 IP 地址或主机名作为参数。您为其提供了一个完整的 URL,它尝试将其解析为主机名并失败。除了完全限定的名称之外的所有内容都被剥离后,该ping
命令能够检查连接(并且在我的以下测试中失败,可能是因为ICMP
请求被阻止或因为服务器已关闭):
$ ping gfe.cit.api.here.com
Pinging cle2-cit.eu-west-1.elasticbeanstalk.com [54.154.19.134] with 32 bytes of data:
Request timed out.
Run Code Online (Sandbox Code Playgroud)
有关ping
和之间的一般区别wget
,请参阅亚历克的回答。
有关解释错误 400 的可能原因,请参阅 roaima 的原因。
回答你实际问的问题。wget
需要一个 URL,但ping
需要一个主机名。您的 URL 由协议 ( https
)、主机名 ( gfe.cit.api.here.com
)、路径 ( /1/layer_put.json
) 和三个编码参数 ( layer_id=123&app_id=x2&app_code=x1
) 组成。URL 中可能会出现其他项目,例如身份验证和端口。
该ping
命令仅接受主机名组件。
wget
然后,该命令打开到协议预期端口的 TCP/IP 连接(443/tcp
在 的情况下https
),并使用 HTTP 协议从指定的 URL 检索内容。
该ping
命令使用 ICMP 向指定主机发送ping请求,然后返回回显回复响应。它们分别是 ICMP 数据包类型 8 和 0。
现在,您wget
失败的根本原因是您没有将参数引用到wget
,因此外壳程序会对其进行解析。&
URL 中间的字符告诉 shell 在后台运行直到该点的所有内容。然后它立即运行该行的其余两个部分,并立即出错。
在您的 URL 周围使用单引号,它可能会按您的预期工作。
所以我们有
wget -O layer_put.json 'https://gfe.cit.api.here.com/1/layer_put.json?layer_id=123&app_id=x2&app_code=x1'
ping -c3 gfe.cit.api.here.com
Run Code Online (Sandbox Code Playgroud)
小智 5
Wget 主要用于通过 HTTP、HTTPS 和 FTP 下载,因此默认情况下将使用 TCP 进行连接。Ping 使用称为ICMP的协议,它基本上是对主机说“你好”并查看它是否响应。ICMP 不使用端口,这就是 http:// 的意思。它告诉“计算机”在端口 80 上打开连接,并为 HTTPS 端口 443。由于 ICMP 不使用端口,ping 只需要 IP 地址,但 wget 建立了 TCP 连接并下载文件。