利用wget和curl命令测试IPv6的网站内容

里小伙伴们天天用的IPv6,到底是什么?

4ipv6.com是同时具有ipv4和ipv6地址的ipv6测试域名,用来检测你域名是否支持ipv6地址。

利用wget和curl命令测试IPv6的网站内容
4ipv6.com

如果返回ipv4地址形式,说明你当前环境是ipv4优先,不一定有ipv6环境。
如果返回ipv6地址形式,说明你当前环境一定支持ipv6访问,而且默认优先选择了ipv6链路。

通过nslookup可以看到如下结果,可知同时具有v4和v6地址:
# nslookup 4ipv6.com
Non-authoritative answer:
Name: 4ipv6.com
Address: 47.75.128.51
Name: 4ipv6.com
Address: 2001:19f0:6001:2df7:5400:2ff:fef0:848b

首先在一个具有ipv6地址的服务器或者具有ipv6环境的机器上利用curl 加-v参数查看详细访问过程:

$ curl -v -6 http://4ipv6.com
* About to connect() to 4ipv6.com port 80 (#0)
* Trying 2001:19f0:6001:2df7:5400:2ff:fef0:848b...
* Connected to 4ipv6.com (2001:19f0:6001:2df7:5400:2ff:fef0:848b) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 4ipv6.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.18.0
< Date: Tue, 20 Oct 2020 06:27:20 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: https://4ipv6.com/
<
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.18.0</center>
</body>
</html>
* Connection #0 to host 4ipv6.com left intact

可以发现系统解析到ipv6地址,并进行ipv6访问。但是http被重定向301了,那么301后是什么呢? curl并没有继续访问下去。这种情况下,要加参数-L 来继续进行访问:

$ curl -v -6 -L http://4ipv6.com
* About to connect() to 4ipv6.com port 80 (#0)
* Trying 2001:19f0:6001:2df7:5400:2ff:fef0:848b...
* Connected to 4ipv6.com (2001:19f0:6001:2df7:5400:2ff:fef0:848b) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 4ipv6.com
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.18.0
< Date: Tue, 20 Oct 2020 06:30:49 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: https://4ipv6.com/
<
* Ignoring the response-body
* Connection #0 to host 4ipv6.com left intact
* Issue another request to this URL: 'https://4ipv6.com/'
* Found bundle for host 4ipv6.com: 0x1be3ee0
* About to connect() to 4ipv6.com port 443 (#1)
* Trying 2001:19f0:6001:2df7:5400:2ff:fef0:848b...
* Connected to 4ipv6.com (2001:19f0:6001:2df7:5400:2ff:fef0:848b) port 443 (#1)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
* subject: CN=4ipv6.com
* start date: Nov 30 00:00:00 2019 GMT
* expire date: Nov 29 12:00:00 2020 GMT
* common name: 4ipv6.com
* issuer: CN=Encryption Everywhere DV TLS CA - G1,OU=www.digicert.com,O=DigiCert Inc,C=US
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 4ipv6.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.18.0
< Date: Tue, 20 Oct 2020 06:30:54 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/7.1.33
<
* Connection #1 to host 4ipv6.com left intact
2409:b240:3800:0:250:56ff:fe90:a650

嗯,发现最终访问到了ipv6地址,并直接返回了结果显示在当前输出终端,结果就是我的客户端ipv6的地址。

如果利用wget来访问:
$ wget -6 http://4ipv6.com
--2020-10-20 14:35:08-- http://4ipv6.com/
Resolving 4ipv6.com (4ipv6.com)... 2001:19f0:6001:2df7:5400:2ff:fef0:848b, 47.75.128.51
Connecting to 4ipv6.com (4ipv6.com)|2001:19f0:6001:2df7:5400:2ff:fef0:848b|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://4ipv6.com/ [following]
--2020-10-20 14:35:09-- https://4ipv6.com/
Connecting to 4ipv6.com (4ipv6.com)|2001:19f0:6001:2df7:5400:2ff:fef0:848b|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [text/html]
Saving to: ‘index.html’

[ <=> ] 35 --.-K/s in 0s

2020-10-20 14:35:18 (2.87 MB/s) - ‘index.html’ saved [35]

wget默认会直接把访问的url地址作为文件名把结果保存下来,查看index.html就是结果。
如果我们想要wget直接在终端输出结果显示,需要用 -q -O -

$ wget -6 -q -O - http://4ipv6.com
2409:b240:3800:0:250:56ff:fe90:a650

这样是比较干净的结果。