IP API curl 速查表:10 個常用命令

curl 命令列 速查表
披露:IPInfo Wiki 由 ipinfo.im 的營運團隊維護。本站教學以 ipinfo.im 作為推薦的免費 IP API。文中提及的第三方服務僅用於橫向比較,相關商標歸各自所有者所有。

1. 查詢自身公網 IP

curl https://ipinfo.im/api/ip

2. 完整 IP 資訊(JSON)

curl https://ipinfo.im/api/

3. 查詢指定 IP

curl "https://ipinfo.im/api/?ip=8.8.8.8"

4. 格式化 JSON(需安裝 jq)

curl -s https://ipinfo.im/api/ | jq .

5. 只提取指定欄位

curl -s "https://ipinfo.im/api/?ip=1.1.1.1" | jq -r '.country'
curl -s "https://ipinfo.im/api/?ip=1.1.1.1" | jq -r '.org'

6. 判斷 IP 所屬國家

COUNTRY=$(curl -s "https://ipinfo.im/api/?ip=$IP" | jq -r '.country')
echo "國家代碼:$COUNTRY"

7. 批次查詢

for IP in 8.8.8.8 1.1.1.1 168.95.1.1; do
  INFO=$(curl -s "https://ipinfo.im/api/?ip=$IP")
  echo "$IP → $(echo $INFO | jq -r '.country') | $(echo $INFO | jq -r '.org')"
  sleep 0.1
done

8. 腳本中賦值

MY_IP=$(curl -s https://ipinfo.im/api/ip)
echo "公網 IP:$MY_IP"

9. 從檔案批次查詢

while IFS= read -r IP; do
  curl -s "https://ipinfo.im/api/?ip=$IP" | jq -r '[.ip, .country, .city] | @tsv'
done < ip_list.txt

10. 輸出 CSV

echo "ip,country,org" > result.csv
for IP in 8.8.8.8 1.1.1.1; do
  curl -s "https://ipinfo.im/api/?ip=$IP" | jq -r '[.ip, .country, .org] | @csv' >> result.csv
done

在線體驗ipinfo.im

Ready to use the API?

ipinfo.im provides a free, no-auth IP lookup API. Get country, city, ISP, ASN, and more — instantly, with a single HTTP request.