获取ipv6
AMD:dhclient -6 ens3
ARM:dhclient -6 enp0s6
脚本文件:
Debian_IPv6() {
iName=$(ip add | grep "^2: " | awk -F'[ :]' '{print $3}')
dhclient -6 $iName #临时开启IPv6
echo $iName #人工查看网卡是否正确
sed -i "$ a iface $iName inet6 dhcp" /etc/network/interfaces
ifup $iName #重新启用网络接口,应用新的IPv6配置
sleep 10s #等待10秒,确保IPv6已启用
ip -6 route add default dev $iName #添加IPv6路由,优先使用IPv6连接
ping -c 5 ipv6.google.com #ping 5次,自动停止ping
pkill ping #停止正在执行的ping进程
if [[ $(curl -sS -6 ifconfig.co) =~ ":" ]]; then
echo "IPv6 is preferred."
else
echo "IPv4 is preferred."
fi
}
Ubuntu_IPv6() {
yamlName=$(find /etc/netplan/ -iname "*.yaml")
iName=$(ip add | grep "^2: " | awk -F'[ :]' '{print $3}')
dhclient -6 $iName
MAC=$(ip add | grep "link/ether.*brd" | awk -F' ' '{print $2}')
IPv6=$(ip add | grep "inet6.*global" | awk -F' ' '{print $2}')
if [[ ${#IPv6} -lt 5 ]]; then echo "Can't IPv6"; exit 1; fi
cp $yamlName /root/
cat <<0099 >$yamlName
network:
ethernets:
ens3:
dhcp4: true
dhcp6: false
match:
macaddress: $MAC
addresses:
- $IPv6
set-name: $iName
version: 2
0099
netplan apply
sleep 10s #等待10秒,确保IPv6已启用
ip -6 route add default dev $iName #添加IPv6路由,优先使用IPv6连接
ping -c 5 ipv6.google.com #ping 5次,自动停止ping
pkill ping #停止正在执行的ping进程
if [[ $(curl -sS -6 ifconfig.co) =~ ":" ]]; then
echo "IPv6 is preferred."
else
echo "IPv4 is preferred."
fi
}
myOS=$(hostnamectl | sed -n 's_.*System: \(\S*\).*_\1_p')
#Ubuntu, Debian
if [[ "$myOS" =~ "Ubuntu" ]]; then
echo "Ubuntu"
Ubuntu_IPv6
elif [[ "$myOS" =~ "Debian" ]]; then
echo "Debian"
Debian_IPv6
fi