curl 模拟访问
curl -o page1.html http://www.example.com/page/1/ -o page2.html http://www.example.com/page/2/
# 只显示 HTTP头部信息,不显示网页文件的内容
curl --user-agent "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" -I http://www.example.com/example.html
# 显示HTTP头部信息和网页文件的内容
curl --user-agent "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" -i http://www.example.com/example.html
# 使用重定向将网页的内容保存到本地文件
curl --user-agent "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" -i http://www.example.com/example.html >index.html
# 使用-o/-O将文件的内容保存到本地文件
curl -o index.html --user-agent "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)" -i http://www.example.com/example.html
# -O URL中的文件名会被用作保存输出的文件名
curl -O --user-agent "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/example.html)" -i http://www.example.com/example.html
# 同时下载多个文件 -o/-O
curl -O http://www.example.com/page/2/ -O http://www.example.com/page/3/
curl -o page1.html http://www.example.com/page/1/ -o page2.html http://www.example.com/page/2/
# -A 自定义user-Agent
# -H 自定义header
# 获取访问URL的时间,包的大小,平均下载速度
## 使用别名来简化命令
alias curlb='curl -s -o /dev/null -w "19-10-25T11:24:10+08:00 %{http_code} %{time_namelookup} %{time_connect} %{time_appconnect} %{time_pretransfer} %{time_redirect} %{time_starttransfer} %{size_request} %{size_download} %{speed_download} %{time_total}\n"'
alias curld='curl -s -o /dev/null -w "
请求时间: 19-10-25T11:24:10+08:00
HTTP状态码: %{http_code}
DNS解析时间: %{time_namelookup} s
建立连接时间: %{time_connect} s
连接完成时间: %{time_appconnect} s
准备传输时间: %{time_pretransfer} s
重定向时间: %{time_redirect} s
传输开始时间: %{time_starttransfer} s
请求数据包大小: %{size_request} Bytes
下载数据包大小: %{size_download} Bytes
平均下载速度: %{speed_download} Bytes/s
--------------
消耗总时长: %{time_total} s \n
"'
curl 案例
# 1.使用curl请求源站,协议为http,IP为1.1.1.1,域名为www.baidu.com。
curl -H "Host:www.baidu.com" http://1.1.1.1
# 2.使用curl请求源站,协议为https,IP为1.1.1.1,域名为www.baidu.com。并且忽略证书
curl -k -H "Host:www.baidu.com" https://1.1.1.1
# 3.使用curl请求源站,协议为http,IP为1.1.1.1,域名为www.baidu.com。只要得到头部信息
curl -I -H "Host:www.baidu.com" http://1.1.1.1
# 4.使用curl请求源站,协议为http,IP为1.1.1.1,域名为www.baidu.com。 只要得到头部信息,源站有可能返回了302跳转,要求跟随跳转
curl -IL -H "Host:www.baidu.com" http://www.baidu.com
# 5.使用curl请求源站,协议为http,IP为1.1.1.1,域名为www.baidu.com。 只要得到头部信息,并且显示详细的请求过程
curl -Ivs -H "Host:www.baidu.com" http://1.1.1.1
curl --trace output.txt -I -H "Host:www.baidu.com" http://1.1.1.1
curl命令判断是否携带SNI
出现502,判断是否因为没有带SNI而导致的出现返回502号界面
# curl带SNI访问https
curl -v --resolve www.baidu.com:443:14.215.177.39 https://www.baidu.com -vv
# 判断curl访问https是否正常
curl -I -H "Host: www.baidu.com" https://14.215.177.39 -vv
## curl这个命令里面的-v --resolve的具体含义:
## --resolve 后面接的是: domain:[443]:ipaddress URL(SSL连接:如“https://)、
## --resolve HOST:PORT:ADDRESS 将 HOST:PORT 强制解析到 ADDRESS
## -v, --verbose 显示详细操作信息
查询本机公网ip
curl myip.ipip.net
curl myipip.net