我现在是通过脚本获取的,截图如下
工作原理是优先逐个从多个网络接口获取(用循环代码遍历网络接口会出错,只能逐个获取,这会显得代码臃肿),当都失败后再通过网卡获取公网IPV6地址,多个公网IPV6地址则获取最后一个,脚本代码如下:
#!/bin/bash
# 依次尝试各个 URL 获取 IPv6 地址
result=$(curl -s --max-time 5 "http://v6.666666.host:66/ip")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "http://myip6.ipip.net")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "http://v6.66666.host:66/ip")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://6.ipw.cn")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://ipv6.ddnspod.com")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://6.666666.host:66/ip")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://ipv6.icanhazip.com")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://api64.ipify.org")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://ident.me")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
result=$(curl -s --max-time 5 "https://speed.neu6.edu.cn/getIP.php")
if [ $? -eq 0 ]; then
ipv6=$(echo "$result" | grep -oE '([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}')
if [ -n "$ipv6" ]; then
echo "$ipv6"
exit 0
fi
fi
# 获取所有 IPv6 地址,过滤掉链路本地地址和本地回环地址
ipv6_addresses=$(ip -6 addr | grep -P 'inet6 [^f]' | awk '{print $2}' | cut -d/ -f1 | grep -v '::1')
if [ -n "$ipv6_addresses" ]; then
last_ipv6=$(echo "$ipv6_addresses" | tail -n 1)
echo "$last_ipv6"
exit 0
fi
echo "获取公网IPV6地址失败"
exit 1
为了减少网卡公网地址过多,我还在计划任务里加入定时重启网卡功能(每8个小时):

具体脚本代码如下,代码上有注释很方便理解
#!/bin/bash
# 获取所有网卡名称,并过滤掉虚拟网卡
network_interfaces=$(ip -o link show | awk -F': ' '{print $2}' | grep -Ev '^(lo|docker|veth|br-|wlp)')
# 检查是否有符合条件的网卡
if [ -z "$network_interfaces" ]; then
echo "未找到需要重启的物理网卡。"
exit 0
fi
# 循环重启每个物理网卡
for interface in $network_interfaces; do
echo "[$interface]网卡: "
# 关闭网卡
ip link set dev "$interface" down
if [ $? -eq 0 ]; then
echo "已关闭,"
else
echo "关闭失败,"
continue
fi
# 等待一段时间,确保网卡完全关闭
sleep 2
# 启动网卡
ip link set dev "$interface" up
if [ $? -eq 0 ]; then
echo "已启动!"
else
echo "启动失败!"
fi
done
最后我的操作就这样,当然这些脚本代码是通过我用AI模型帮我写的,然后通过我一些浅薄的知识修整下! |