Skip to content

Latest commit

 

History

History
256 lines (187 loc) · 6.69 KB

File metadata and controls

256 lines (187 loc) · 6.69 KB

命令行技巧 · ipyou.net

无需注册、无需 API Key,curl 直接用。对外只有两个接口

curl ipyou.net              # 查你自己的 IP
curl ipyou.net/1.1.1.1      # 查指定 IP

Accept: application/json 就返回 JSON,其余用法都是围绕这两个地址组合出来的。


基础

curl ipyou.net                                        # 纯文本
curl -H "Accept: application/json" ipyou.net          # JSON
curl ipyou.net/1.1.1.1                                # 指定 IP

命令行客户端(curl / wget / httpie)默认拿纯文本;浏览器打开同样地址是网页版。 不需要 -Lhttphttps 都行。

wget -qO- ipyou.net         # wget 同样可用
http ipyou.net              # HTTPie

用别的 HTTP 库时不必伪装 User-Agent —— 带上 Accept 头即可application/json 拿 JSON,text/plain 拿纯文本。


脚本里取 IP

# 装了 jq
MYIP=$(curl -s -H "Accept: application/json" ipyou.net | jq -r .ip)

# 没有 jq:直接解析纯文本首行
MYIP=$(curl -s ipyou.net | sed -n 's/^IP[[:space:]]*:[[:space:]]*//p' | head -1)

# 带超时与失败退出(推荐写法)
ip=$(curl -s --max-time 5 ipyou.net | sed -n 's/^IP[[:space:]]*:[[:space:]]*//p' | head -1)
[ -n "$ip" ] || { echo "取 IP 失败" >&2; exit 1; }

# DDNS / 定时任务里对比上次记录
new="$ip"
old=$(cat ~/.last_ip 2>/dev/null || true)
if [ "$new" != "$old" ]; then
    echo "$new" > ~/.last_ip
    echo "IP 变了: $old -> $new"
    # 这里触发你的通知 / DNS 更新
fi

代理 / 节点相关

# 直连 vs 走代理,确认代理确实生效
curl -s ipyou.net
curl -s --proxy socks5h://127.0.0.1:1080 ipyou.net
curl -s --proxy http://127.0.0.1:7890 ipyou.net

# 指定网卡出口(多网卡 / 多拨)
curl -s --interface eth1 ipyou.net

# 只用 IPv4 / 只用 IPv6
curl -s -4 ipyou.net
curl -s -6 ipyou.net

盯着代理出口漂不漂(配合本仓库脚本):

./scripts/ipwatch.sh 10 -x socks5h://127.0.0.1:1080

判断 IP 属性

需要 jq

API='curl -s -H Accept:application/json'

# 我的 IP 是不是机房 IP
$API ipyou.net | jq -r '.usage_inferred.label'
# → 家庭宽带 / 移动网络 / 数据中心 / 教育政府

# 风控值与等级
$API ipyou.net/1.1.1.1 | jq -r '.ip_score | "\(.score) \(.level)"'

# 是否住宅 IP(适合养号的类型)
$API ipyou.net/1.1.1.1 | jq -r '
  if (.usage_inferred.label // "") | test("家庭宽带|移动网络")
  then "residential" else "not-residential" end'

# 是否命中黑名单
$API ipyou.net/1.1.1.1 | jq -r '.blocklisted'

# TikTok 场景星级
$API ipyou.net/1.1.1.1 | jq -r '.scenarios[] | select(.name=="TikTok") | .stars'

在 CI / 脚本里做「非住宅就退出」的守卫:

type=$(curl -s --max-time 10 -H "Accept: application/json" ipyou.net \
        | jq -r '.usage_inferred.label')
case "$type" in
  家庭宽带|移动网络) echo "住宅 IP,继续" ;;
  *) echo "当前是 $type,中止" >&2; exit 1 ;;
esac

查一批 IP

没有批量接口,循环调用单个接口即可 —— 记得留间隔,别把公共服务打爆:

# 最简单
while read -r ip; do curl -s "ipyou.net/$ip"; sleep 1; done < ips.txt

# 只列出「机房 IP」
while read -r ip; do
  t=$(curl -s -H "Accept: application/json" "ipyou.net/$ip" | jq -r '.usage_inferred.label // "-"')
  [ "$t" = "数据中心" ] && echo "$ip"
  sleep 1
done < ips.txt

# 导出 CSV(用本仓库脚本,已处理去重 / 限速 / 429 退避重试)
./scripts/batch-check.sh ips.txt > result.csv
./scripts/batch-check.sh -t ips.txt      # 人眼友好的表格
IPYOU_SLEEP=2 ./scripts/batch-check.sh ips.txt   # 放慢一点

ips.txt 支持这些写法,脚本会自动提取 IP、去重、跳过注释:

# 我的代理池
8.8.8.8
1.1.1.1:8080
user:pass@203.0.113.9:3128
202.96.128.86

实用小工具

# 加到 ~/.bashrc / ~/.zshrc,随时 myip
alias myip='curl -s ipyou.net'

# 每 5 秒刷新显示出口 IP
watch -n5 'curl -s ipyou.net | head -1'

# 只在 IP 变化时提醒(本仓库脚本,比 watch 干净)
./scripts/ipwatch.sh 10

# 变化时发系统通知(macOS)
./scripts/ipwatch.sh 10 | while read -r line; do
  osascript -e "display notification \"$line\" with title \"IP 变化\""
done

# 记录一天的出口 IP 变化到日志
./scripts/ipwatch.sh 30 >> ~/ip-changes.log 2>&1 &

SSH 登录后自动显示当前出口 IP(加到服务器 ~/.bashrc):

command -v curl >/dev/null && curl -s --max-time 3 ipyou.net 2>/dev/null | head -1

Windows

PowerShell

# 纯文本
(Invoke-WebRequest "https://ipyou.net" -UseBasicParsing `
   -Headers @{ "Accept" = "text/plain" }).Content

# JSON(对象,可继续取字段)
$d = Invoke-RestMethod "https://ipyou.net/1.1.1.1" -Headers @{ "Accept" = "application/json" }
$d.usage_inferred.label
$d.ip_score.score

# 或直接用本仓库脚本
.\scripts\ipyou.ps1                       # 本机信息
.\scripts\ipyou.ps1 -Quiet                # 只取 IP
.\scripts\ipyou.ps1 1.1.1.1               # 查指定 IP
.\scripts\ipyou.ps1 8.8.8.8,1.1.1.1 -Table
.\scripts\ipyou.ps1 -Watch 10             # 监控出口 IP

CMD(Windows 10+ 自带 curl):

curl ipyou.net
curl ipyou.net/1.1.1.1

Python

python3 scripts/ipyou.py            # 本机信息
python3 scripts/ipyou.py -q         # 只取 IP
python3 scripts/ipyou.py 1.1.1.1
python3 scripts/ipyou.py -b ips.txt > out.csv    # 循环查一批 → CSV

作为库导入(零依赖,只用标准库):

from ipyou import myip, details, batch, is_residential

print(myip())                            # '1.2.3.4'
d = details('1.1.1.1')
print(d['usage_inferred']['label'], d['ip_score']['score'])
print(is_residential('202.96.128.86'))   # True

for row in batch(['8.8.8.8', '1.1.1.1']):  # 循环调用,自动留间隔
    print(row['ip'], row['usage_inferred']['label'])

注意事项

  • 加超时--max-time 10,别让脚本卡死。
  • 有防滥用限流:超限返回 429Retry-After。循环调用请加间隔(建议 ≥1 秒)。
  • 查自己的 IP 不走缓存,换网络 / 切代理立刻返回新结果;查指定 IP 的结果有缓存,重复查很快。
  • 想换自建地址:设 IPYOU_BASE 环境变量,所有脚本都认。