网络工具 / 命令文档
curl 命令
概述
Linux系统运维必备工具之一
curl 基本概念
curl = Client URL,是一个命令行工具,用于在各种协议之间传输数据
核心功能:
发送HTTP/HTTPS请求
上传下载文件
测试API接口
网站健康检查
自动化脚本集成
为什么选择curl? • 跨平台支持 • 支持多种协议(HTTP, HTTPS, FTP, SMTP等) • 轻量级,无需GUI • 脚本友好,易于自动化
基本语法结构
curl [选项] [URL]
# 最简单的用法
curl https://www.baidu.com
# 带参数的完整格式
curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://api.example.com/data核心参数详解
| 参数 | 功能说明 | 实例 |
|---|---|---|
| -X, --request | 指定HTTP方法 | -X POST, -X GET, -X DELETE |
| -H, --header | 添加HTTP头部 | -H "Authorization: Bearer token" |
| -d, --data | 发送POST数据 | -d "name=value&age=25" |
| -o, --output | 保存输出到文件 | -o result.json |
| -s, --silent | 静默模式,不显示进度 | -s |
| -v, --verbose | 详细输出调试信息 | -v |
| -I, --head | 只获取HTTP头部 | -I |
| -L, --location | 跟随重定向 | -L |
| -k, --insecure | 忽略SSL证书错误 | -k |
| --connect-timeout | 连接超时时间 | --connect-timeout 10 |
运维实战场景
# 检查网站是否正常
curl -I -s -o /dev/null -w "%{http_code}" http://img.youngsu.top/
# 批量检查多个服务
for url in "http://img.youngsu.top" "http://gips0.baidu.com/it/u=1690853528,2506870245&fm=3028&app=3028&f=JPEG&fmt=auto?w=1024&h=1024"; do status=$(curl -I -s -o /dev/null -w "%{http_code}" "$url") echo "$url: $status"; done1. 服务健康检查
# GET请求获取数据
curl -H "Accept: application/json" \
"https://api.github.com/users/octocat"
# POST请求提交数据
curl -X POST \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"123456"}' \
https://api.example.com/login
# 带认证的API调用
curl -H "Authorization: Bearer YOUR_TOKEN" \
https://api.example.com/protected/data2. API接口测试
# 下载文件并显示进度 curl -O https://example.com/large-file.zip # 断点续传下载 curl -C - -O https://example.com/large-file.zip # 限速下载(限制为100KB/s) curl --limit-rate 100k -O https://example.com/file.zip
3. 文件下载管理
# 详细的性能时间分析
curl -w "DNS解析: %{time_namelookup}s\n连接时间: %{time_connect}s\n总时间: %{time_total}s\n" \
-o /dev/null -s https://www.example.com
# 创建性能监控脚本
curl -w "@curl-format.txt" -o /dev/null -s https://api.example.com
# curl-format.txt内容:
# time_namelookup: %{time_namelookup}\n
# time_connect: %{time_connect}\n
# time_appconnect: %{time_appconnect}\n
# time_total: %{time_total}\n4. 性能监控分析
高级用法技巧
Cookie处理
# 保存Cookie curl -c cookies.txt https://example.com/login # 使用Cookie curl -b cookies.txt https://example.com/dashboard # 一次性使用Cookie curl -b "sessionid=abc123; userid=456" https://example.com/api
认证方式
# Basic认证 curl -u username:password https://api.example.com # Bearer Token curl -H "Authorization: Bearer eyJhbGciOi..." https://api.example.com # 证书认证 curl --cert client.pem --key client.key https://secure-api.com
安全提示:在生产环境中,避免在命令行中直接暴露敏感信息,建议使用环境变量或配置文件
服务状态监控脚本
#!/bin/bash
# 服务健康检查脚本
services=(
"https://api.example.com/health"
"https://web.example.com"
"https://db-api.example.com/status"
)
for service in "${services[@]}"; do
response=$(curl -s -o /dev/null -w "%{http_code}" --connect-timeout 10 "$service")
if [ "$response" = "200" ]; then
echo "$service - 正常"
else
echo "$service - 异常 (HTTP: $response)"
# 这里可以添加告警逻辑
fi
doneAPI压测脚本
#!/bin/bash
# 简单的API压力测试
url="https://api.example.com/test"
concurrent=10
requests=100
echo "开始压力测试: $concurrent 并发, $requests 请求"
seq 1 $requests | xargs -n1 -P$concurrent -I{} curl -s -o /dev/null -w "%{time_total}\n" "$url" | \
awk '{sum+=$1; count++} END {printf "平均响应时间: %.3f秒\n", sum/count}'学习总结
掌握要点: • curl是运维工程师必备的网络工具 • 熟练掌握常用参数和选项 • 能够编写自动化监控脚本 • 理解HTTP协议和API测试方法 • 注意安全性,保护敏感信息
持续练习建议:每天使用curl进行API测试和服务监控,逐步提升运维自动化水平!