curl 网络测试常用方法

一、基础网络测试

1. 连通性测试
/* by 01130.hk - online tools website : 01130.hk/zh/escape.html */
curl -I http://example.com  # 仅显示响应头(快速检查状态码)
curl -v https://api.example.com  # 显示详细过程(握手、头信息)
2. 超时控制
/* by 01130.hk - online tools website : 01130.hk/zh/escape.html */
curl --connect-timeout 5 -m 10 http://example.com  # 连接超时5秒,总超时10秒
3. DNS解析测试
curl --resolve example.com:80:1.2.3.4 http://example.com  # 强制指定IP解析

二、性能分析

1. 速度测试
curl -o /dev/null -s -w "时间: %{time_total}s\n速度: %{speed_download}B/s" https://example.com/file.zip
2. 连接阶段分析
curl -w "
DNS解析: %{time_namelookup}s
建立连接: %{time_connect}s
SSL握手: %{time_appconnect}s
首字节: %{time_starttransfer}s
总时间: %{time_total}s\n" -o /dev/null -s https://example.com

三、特殊场景

1. HTTPS证书检查
curl -vI https://example.com 2>&1 | grep -E "SSL|CN"  # 查看证书信息
curl --cacert ./ca.pem https://intranet.com  # 自定义CA证书
2. 代理测试
curl -x http://proxy:8080 -U user:pass http://example.com  # 通过代理访问
3. API调试
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com
curl -H "Authorization: Bearer token" https://api.example.com/data
4. 文件传输
curl -u ftpuser:ftppass -T file.txt ftp://ftp.example.com # FTP上传 curl -O http://example.com/image.jpg # 下载文件保存原名

四、高级诊断

1. IPv4/IPv6强制
curl -4 http://example.com  # 强制IPv4
curl -6 http://example.com  # 强制IPv6
2. Cookie操作
curl -c cookies.txt http://login.com  # 保存Cookie
curl -b cookies.txt http://dashboard.com  # 携带Cookie
3. 流量控制
curl --limit-rate 100K http://example.com/largefile.iso  # 限速100KB/s

五、实用组合场景

场景命令示例
CDN节点检测curl -s -o /dev/null -w "%{remote_ip}" http://cdn.com
重定向跟踪curl -Ls -w "最终URL: %{url_effective}" -o /dev/null http://bit.ly/short
负载均衡测试`for i in {1..10}; do curl -s lb.example.comgrep Server; done`
压缩测试`curl -H "Accept-Encoding: gzip" -I example.comgrep Content-Encoding`
持续监控while true; do curl -s -o /dev/null -w "%{http_code} %{time_total}\n" http://service; sleep 2; done

六、常用输出格式化变量

curl -w "
响应码: %{http_code}
重定向URL: %{redirect_url}
SSL验证: %{ssl_verify_result}
本地IP: %{local_ip}
远端IP: %{remote_ip}
下载大小: %{size_download} bytes
" -o /dev/null -s http://example.com

调试技巧:使用 -v 时,> 表示发送数据,< 表示接收数据,* 表示附加信息。结合 grep 可快速过滤关键信息(如 grep -E '> Host:|< HTTP')。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值