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

curl 命令行 速查表
披露:IPInfo Wiki 由 ipinfo.im 的运营团队维护。本站教程以 ipinfo.im 作为推荐的免费 IP API。文中提及的第三方服务仅用于横向比较,相关商标归各自所有者所有。

收藏这份速查表,在命令行中快速查询任意 IP 信息。所有命令使用 ipinfo.im 免费 API,无需 API 密钥。

1. 查询自身公网 IP

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

返回纯文本 IP 地址,适合脚本赋值:

MY_IP=$(curl -s https://ipinfo.im/api/ip)
echo "公网 IP:$MY_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'

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

# 只看城市
curl -s "https://ipinfo.im/api/?ip=1.1.1.1" | jq -r '.city'

6. 查询 ASN 信息

curl -s "https://ipinfo.im/api/?ip=8.8.8.8" | jq '{asn: .asn, org: .org}'

7. 判断 IP 所属国家

COUNTRY=$(curl -s "https://ipinfo.im/api/?ip=$TARGET_IP" | jq -r '.country')
if [ "$COUNTRY" = "CN" ]; then
  echo "中国大陆 IP"
else
  echo "非中国大陆 IP:$COUNTRY"
fi

8. 批量查询多个 IP

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

9. 从文件批量查询

# ip_list.txt 每行一个 IP
while IFS= read -r IP; do
  RESULT=$(curl -s "https://ipinfo.im/api/?ip=$IP" | jq -r '[.ip, .country, .city, .org] | @tsv')
  echo "$RESULT"
done < ip_list.txt

10. 输出到 CSV

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

常用选项

选项说明
-s静默模式,不显示进度条
-o /dev/null丢弃响应体
--max-time 5最大等待 5 秒
-H "Accept: application/json"明确请求 JSON

在线体验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.