IP地址API与命令行接口
在脚本、Shell或定时任务中获取您的公网IP地址:供curl使用的纯文本、带详细信息的JSON,以及在需要特定协议时使用的仅IPv4和仅IPv6主机。无需密钥,无需账户,每个地址每分钟600次请求。
快速开始:用curl获取IP
curl checkmyip.com/raw
203.0.113.42对大多数用途来说,这就是整个API:一次请求,一个地址,一个换行。curl -4和curl -6可以强制指定协议;curl checkmyip.com/json返回下文所述的详细信息。任何发送Accept: text/plain或命令行User-Agent的客户端,都会从/raw得到纯地址;浏览器则得到给人看的页面。
$ curl checkmyip.com
216.73.216.52
$ curl -H "Accept: text/plain" https://checkmyip.com/
216.73.216.52 接口
| 接口 | 返回 | 说明 |
|---|---|---|
GET https://checkmyip.com/raw | 您的IP,纯文本,末尾带换行 | 使用curl/wget/PowerShell的User-Agent时GET /亦可 |
GET https://checkmyip.com/ip · /ip.txt | 同样的纯文本地址(供期望这些路径的脚本使用的别名) | Cache-Control: no-store |
GET https://checkmyip.com/json | JSON:ip、version、geo、network、type、risk摘要(字段见下文) | Cache-Control: private, no-store |
GET https://checkmyip.com/json?ip=203.0.113.42 | 任意公网地址的同样JSON | 同样的600次/分钟限制 |
GET https://ipv4.checkmyip.com/ip | 仅通过IPv4返回{ip, family: 4, …} | 没有IPv4路径时会失败 |
GET https://ipv6.checkmyip.com/ip | 仅通过IPv6返回{ip, family: 6, …} | 没有IPv6路径时会失败 |
GET https://ds.checkmyip.com/ip | 两者都可用时您的客户端偏好的协议 | ?format=text可输出纯地址 |
所有接口都通过HTTPS应答,为只读用途启用了CORS,发送Cache-Control: no-store,并对搜索引擎标记为noindex。其他地址的查询结果会缓存十分钟。
https://checkmyip.com/rawhttps://checkmyip.com/iphttps://checkmyip.com/ip.txthttps://checkmyip.com/https://checkmyip.com/jsonhttps://checkmyip.com/json?ip=203.0.113.9All of them send Cache-Control: private, no-store, Access-Control-Allow-Origin: * and X-Robots-Tag: noindex; 无需api密钥.
JSON响应字段
ip——字符串形式的地址。version——4或6。geo.consensus——country(ISO 3166-1 alpha-2)、region、city、lat、lon、timezone、confidence(0–1)、disagreement(布尔值)、anycast(布尔值);geo.sources[]——每个数据库的答案,附updated、licence和attribution。network——asn、as_name、as_org、prefix、rir、allocation_date、registry_country、abuse_contacts[]。hostname——PTR名称或null。type.type——residential、mobile、business、datacenter或unknown,附confidence和reasons[]。risk——is_proxy、is_vpn、is_tor、is_datacenter、is_relay、score(0–100)、level、reasons[]、blacklists[]和score_version。缺失的值为null,原因在status或note中;启发式判定会明确说明。如果您向用户展示地理位置数据,必须显示attribution字符串(CC BY 4.0 / CC BY-SA 4.0的条件)。
Two fields are additive on top of the list above: schema_version (the lookup service's response schema, 1.1 today; fields are only ever added) and geo.agreement — the per-field tally behind the consensus (country_sources, country_source_ids, city_sources, city_source_ids, the disagreeing ids and a one-line summary such as “country confirmed by 3 sources, city from 2”). network.origins[] lists every ASN that originates the address (asn, as_name, as_org, named_by[], primary; network.multi_origin is true when there is more than one and network.note says which was chosen as primary). risk.factors[] carries the score arithmetic (id, label, points, source; sum = score before the cap at 100) and risk.reasons[] the same signals as full sentences. A tied city vote sets geo.consensus.city to null and lists the options in geo.consensus.city_candidates[]; geo.consensus.tie_break says what rule applied. hosted_domains (full lookups only) is the IP2WHOIS hosted-domain block: status (ok, cached, budget_exhausted with retry_after, no-data, skipped on fast answers), total_domains, domains[] (a sample — the free plan returns one name per page), page/total_pages and the attribution string, which must be shown with the names. Own-address answers also carry colo (the Cloudflare data centre that served you) and cache (live or edge ≤10 min).
代码示例
每个示例都会打印运行它的那台机器的公网地址。
curl
curl -s https://checkmyip.com/raw
curl -s -4 https://checkmyip.com/raw # 仅IPv4
curl -s -6 https://checkmyip.com/raw # 仅IPv6
curl -s https://checkmyip.com/json | jq .geo.consensus.countrywget
wget -qO- https://checkmyip.com/rawPowerShell
(Invoke-WebRequest -Uri https://checkmyip.com/raw -UseBasicParsing).Content.Trim()
(Invoke-RestMethod -Uri https://checkmyip.com/json).network.asnPython
import urllib.request
ip = urllib.request.urlopen("https://checkmyip.com/raw", timeout=5).read().decode().strip()
print(ip)JavaScript(fetch)
const r = await fetch("https://checkmyip.com/json", { cache: "no-store" });
const { ip, geo } = await r.json();
console.log(ip, geo.consensus.country);Go
resp, err := http.Get("https://checkmyip.com/raw")
if err != nil { log.Fatal(err) }
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
fmt.Println(strings.TrimSpace(string(b)))Bash脚本与cron
#!/bin/sh
# 公网IP变化时发出通知(通过cron每5分钟运行一次)
new=$(curl -fsS --max-time 5 https://checkmyip.com/raw) || exit 0
old=$(cat /var/tmp/public-ip 2>/dev/null)
[ "$new" != "$old" ] && { echo "$new" > /var/tmp/public-ip; logger "public IP changed: $old -> $new"; }请使用--max-time,把失败当作未知而不是已变化,轮询频率不要超过每分钟一次。
速率限制与合理使用
/raw、/ip、/ip.txt、/json、/json?ip=以及按协议区分的/ip主机允许每个地址每分钟600次请求;端口检测允许每分钟20次,且只测试调用方自己的地址。超出限制会返回HTTP 429和JSON格式的错误正文。在这些限制之内,接口可免费用于个人、内部和商业用途;如果您预计会持续超出这些限制,或希望把我们的数据嵌入产品,请先通过联系页面告知我们。请不要在脚本中使用给人看的页面——它们比上述接口更重、更慢。
可用性与状态
接口运行在与网站相同的边缘网络上,并持续受到监控;我们测试服务器的/healthz接口会报告其版本和运行时间。公开的状态页面已在计划中。响应格式的变更会提前在本页公布;现有字段绝不重命名,只会增加。
常见问题
有免费的IP API吗?
有。本页的接口无需密钥或账户,即可以纯文本或JSON返回您的公网地址,但须遵守上述速率限制;/json?ip=则可为任意公网地址返回同样的JSON。IP地址查询页面向人展示的是同样的数据。
IP抓取器违法吗?
“IP抓取器”是一种链接或图片,谁打开它就记录谁的地址。记录访问您自己网站的访客的地址是合法且常规的;诱骗他人泄露地址以便骚扰、定位或攻击他们,才会触及法律。我们的接口只返回调用它的客户端自己的地址。
API有IP地址吗?
每一台应答API的服务器都有IP地址,我们的也不例外。这个问题背后通常真正想问的是“程序能知道自己的公网地址吗?”——这正是这些接口的用途:程序向我们发问,我们回复我们看到请求所来自的地址。
什么是“我的IP地址”API?
它是一个Web接口,谁调用它,它就回复谁的地址。https://checkmyip.com/raw以文本形式返回地址,https://checkmyip.com/json则附带位置、网络和风险详情,文档见上文。
curl ifconfig.me是做什么的?
ifconfig.me是另一个用curl访问时返回调用方地址的网站。curl checkmyip.com/raw在这里做的是同样的事;JSON接口还会额外提供网络和位置字段。
如何用curl查看我的IP地址?
在任何能联网的终端中运行curl -s https://checkmyip.com/raw。加上-4或-6可强制使用IPv4或IPv6。输出是地址加一个换行符,可以安全地在脚本中使用。
ifconfig.me安全吗?
我们无法为其他服务担保。就我们的服务而言:请求的应答不会保存您的地址,响应通过HTTPS传输,隐私政策对接口的适用方式与对页面完全相同。