如何在台湾提升拨号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 安排!