如何在台湾动态拨号VPS上提取公网IP?
如何在台湾动态拨号VPS上提取公网IP?
在台湾动态拨号VPS(Dynamic Dialing VPS)上,提取 当前公网 IP 是常见需求,主要用于 远程连接、网络调试、代理服务 等。由于动态拨号 VPS 每次拨号都会更换 IP,用户需要 实时获取最新的公网 IP。
一、使用 Shell/命令行 获取公网 IP
1?、Linux VPS:使用 curl 或 wget 获取 IP
步骤:
使用 curl 命令
curl -s ifconfig.me
或
curl -s ipinfo.io/ip
使用 wget 命令
wget -qO- ifconfig.me
或
wget -qO- ipinfo.io/ip
说明:
ifconfig.me / ipinfo.io/ip 这些 API 可以返回 当前 VPS 的公网 IP。
适用于大部分 Linux 发行版(CentOS、Ubuntu、Debian)。
2?、Windows VPS:使用 PowerShell 获取 IP
如果你的台湾拨号 VPS 是 Windows 系统,可以使用 PowerShell 查询当前公网 IP。
方法 1:PowerShell + API 查询
(Invoke-WebRequest -Uri "https://ifconfig.me").Content
或
(Invoke-WebRequest -Uri "https://ipinfo.io/ip").Content
方法 2:使用 nslookup
nslookup myip.opendns.com resolver1.opendns.com
说明:
这两种方法均可以提取 当前公网 IP。
适用于 Windows Server 2016/2019/2022 及 Windows 10/11 VPS。
二、使用网络接口命令获取公网 IP
有时,VPS 可能会有多个 网卡接口,可以使用 ip 或 ifconfig 命令获取当前 出口 IP。
1?、Linux 系统
ip addr show eth0 | grep "inet " | awk '{print $2}' | cut -d/ -f1
或
ifconfig eth0 | grep "inet " | awk '{print $2}'
说明:
eth0 是默认网卡名称,不同 VPS 可能会有不同的网卡名称(如 ens33、venet0)。
这个方法可以提取 本机分配的公网 IP,但某些情况下 IP 可能是 私网地址(NAT VPS)。
三、Python 脚本自动获取公网 IP
如果你想要 定期提取公网 IP,可以使用 Python 编写脚本,自动查询并保存 IP 地址。
import requests
# API 网址
url = "https://ifconfig.me"
# 获取公网 IP
public_ip = requests.get(url).text.strip()
print(f"当前公网 IP: {public_ip}")
说明:
适用于 Linux 和 Windows VPS,需要安装 requests 库(pip install requests)。
可以加入 定时任务(crontab / Windows 任务计划),定时检测公网 IP。
四、使用 DDNS(动态域名解析)绑定公网 IP
如果你的 VPS 频繁更换公网 IP,可以使用 DDNS(动态域名解析) 绑定到域名,让你随时访问 VPS。
1?、Cloudflare API 更新 DNS 记录
Cloudflare 提供 API,可以自动更新 域名解析记录,绑定动态 IP。
示例:Linux Shell 自动更新 Cloudflare IP
CLOUDFLARE_ZONE_ID="你的ZoneID"
CLOUDFLARE_API_TOKEN="你的API密钥"
RECORD_NAME="yourdomain.com"
# 获取公网 IP
IP=$(curl -s ifconfig.me)
# 更新 Cloudflare 记录
curl -X PUT "https://api.cloudflare.com/client/v4/zones/$CLOUDFLARE_ZONE_ID/dns_records/$RECORD_ID" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \
--data '{"type":"A","name":"'"$RECORD_NAME"'","content":"'"$IP"'","ttl":120}'
说明:
适用于 台湾拨号 VPS 频繁更换 IP 的情况。
Cloudflare 免费版也可以使用 DDNS 功能。
五、总结
方法 适用系统 适用场景 命令示例
Shell 获取公网 IP Linux 快速查看公网 IP curl -s ifconfig.me
PowerShell 获取公网 IP Windows 快速查询 VPS IP (Invoke-WebRequest -Uri "https://ifconfig.me").Content
检查网卡 IP Linux 本机网卡分配的 IP` ip addr show eth0
Python 自动获取 IP Linux / Windows 定期检测 IP requests.get("https://ifconfig.me").text.strip()
DDNS 绑定动态 IP 任何系统 绑定域名,动态解析 Cloudflare API
推荐方案:
临时查询 IP curl -s ifconfig.me
Windows 远程 VPS 查询 (Invoke-WebRequest -Uri "https://ifconfig.me").Content
自动更新动态 IP 使用 Python + DDNS 绑定域名(适合长期使用)。
如果你需要长期使用台湾动态拨号 VPS,并希望稳定访问,可以考虑 DDNS 方案!