萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux curl 命令大全:從基礎下載到進階 API 測試、斷點續傳與效能分析

curl 核心語法

curl [options] [URL...]

高頻參數速查表

短參數長參數說明
-o--output輸出到指定檔案
-O--remote-name用遠端檔名存檔
-C ---continue-at -斷點續傳
-L--location跟隨重導向
-H--header自定義請求頭
-A--user-agent設定 User-Agent
-b--cookie發送 Cookie
-c--cookie-jar儲存 Cookie 到檔案
-D--dump-header儲存回應頭到檔案
-X--request指定 HTTP 方法 (GET/POST/PUT/DELETE)
-d--dataPOST 資料 (application/x-www-form-urlencoded)
-F--formmultipart/form-data (檔案上傳)
-u--userBasic Auth 帳號:密碼
-x--proxy代理伺服器 [protocol://]host:port
-k--insecure跳過 TLS 憑證驗證
-s--silent靜默模式 (不顯示進度/錯誤)
-S--show-error配合 -s 顯示錯誤
-w--write-out自定義輸出格式 (效能分析神器)
-#--progress-bar進度條顯示
-I--head僅獲取 HEAD 回應
-v--verbose詳細溝通過程

實戰範例集

1. 基礎下載與存檔

# 直接輸出到終端
curl https://example.com

# 存檔 (兩種方式)
curl -o page.html https://example.com
curl -O https://example.com/file.zip

2. 斷點續傳

curl -C - -O https://example.com/large.iso

3. 只取 HTTP 狀態碼 (腳本監控常用)

curl -o /dev/null -s -w "%{http_code}\n" https://example.com

4. 代理與偽裝

# HTTP 代理
curl -x 192.168.1.100:3128 https://example.com

# 偽裝 User-Agent
curl -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" https://example.com

# 偽造 Referer (防盜鏈)
curl -e "https://example.com/page.html" https://example.com/image.jpg

5. Cookie 會話維持

# 登入並儲存 Cookie
curl -c cookies.txt -d "user=foo&pass=bar" https://example.com/login

# 後續請求攜帶 Cookie
curl -b cookies.txt https://example.com/profile

6. 檔案上傳 (POST multipart)

curl -F "file=@/path/to/local.png" -F "name=avatar" https://example.com/upload

7. FTP 下載/上傳

# 下載
curl -O -u user:pass ftp://ftp.example.com/file.txt
# 上傳
curl -T local.txt -u user:pass ftp://ftp.example.com/remote.txt

8. 批次下載 (大括號展開)

curl -O "https://example.com/img[1-10].jpg"
curl -O "https://example.com/{a,b,c}/file.txt"

9. 分塊下載並合併 (大檔案並行加速)

curl -r 0-10485759 -o part1 https://example.com/big.iso
curl -r 10485760-20971519 -o part2 https://example.com/big.iso
curl -r 20971520- -o part3 https://example.com/big.iso
cat part* > big.iso

10. 效能計時與頻寬測試 (核心技巧)

# 僅顯示下載速度
curl -Lo /dev/null -skw "%{speed_download}\n" https://mirrors.163.com/ubuntu/ls-lR.gz

# 完整時間分解
curl -Lo /dev/null -skw "
time_connect: %{time_connect} s
time_namelookup: %{time_namelookup} s
time_pretransfer: %{time_pretransfer} s
time_starttransfer: %{time_starttransfer} s
time_redirect: %{time_redirect} s
speed_download: %{speed_download} B/s
time_total: %{time_total} s
" https://www.sina.com

輸出範例:

time_connect: 0.154 s
time_namelookup: 0.150 s
time_pretransfer: 0.154 s
time_starttransfer: 0.163 s
time_redirect: 0.157 s
speed_download: 324679.000 B/s
time_total: 1.692 s

進階應用:API 測試腳本模板

#!/bin/bash
API="https://api.example.com/v1"
TOKEN="Bearer xxx"

curl -s -X POST "$API/orders" \
  -H "Authorization: $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"item_id":123,"qty":2}' \
  -w "\nHTTP %{http_code} | Total: %{time_total}s | Size: %{size_download}B\n" \
  -o response.json

cat response.json | jq .

常見坑點提醒

  • -d 會自動加 Content-Type: application/x-www-form-urlencoded;送 JSON 需手動 -H "Content-Type: application/json"。
  • -F 會自動設 multipart/form-data 並產生 boundary。
  • 重導向預設不跟隨,需加 -L。
  • 自簽憑證環境必加 -k,生產環境請改用 --cacert 指定 CA。
  • 大檔案上傳建議加 --limit-rate 避免佔滿頻寬。
copyright © 萬盛學電腦網 all rights reserved