core: 处理云电脑 dhcp 获取的地址无法上网

This commit is contained in:
bin456789 2023-11-08 00:23:05 +08:00
parent 2993f1c2cb
commit 6f101bb3c7
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

View File

@ -20,6 +20,10 @@ else
ipv6_dns2='2001:4860:4860::8888' ipv6_dns2='2001:4860:4860::8888'
fi fi
get_first_ipv4_addr() {
ip -4 -o addr show scope global dev eth0 | head -1 | awk '{print $4}'
}
is_have_ipv4_addr() { is_have_ipv4_addr() {
ip -4 addr show scope global dev eth0 | grep -q inet ip -4 addr show scope global dev eth0 | grep -q inet
} }
@ -62,8 +66,8 @@ done
is_have_ipv4_addr && dhcpv4=true || dhcpv4=false is_have_ipv4_addr && dhcpv4=true || dhcpv4=false
is_have_ipv6_addr && dhcpv6_or_slaac=true || dhcpv6_or_slaac=false is_have_ipv6_addr && dhcpv6_or_slaac=true || dhcpv6_or_slaac=false
# 设置静态地址 add_missing_ipv4_config() {
if [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ]; then if [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ]; then
if ! is_have_ipv4_addr; then if ! is_have_ipv4_addr; then
ip -4 addr add "$ipv4_addr" dev eth0 ip -4 addr add "$ipv4_addr" dev eth0
fi fi
@ -72,9 +76,11 @@ if [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ]; then
# 如果 dhcp 无法设置onlink网关那么在这里设置 # 如果 dhcp 无法设置onlink网关那么在这里设置
ip -4 route add default via "$ipv4_gateway" dev eth0 onlink ip -4 route add default via "$ipv4_gateway" dev eth0 onlink
fi fi
fi fi
}
if [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then add_missing_ipv6_config() {
if [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then
if ! is_have_ipv6_addr; then if ! is_have_ipv6_addr; then
ip -6 addr add "$ipv6_addr" dev eth0 ip -6 addr add "$ipv6_addr" dev eth0
fi fi
@ -83,7 +89,12 @@ if [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then
# 如果 dhcp 无法设置onlink网关那么在这里设置 # 如果 dhcp 无法设置onlink网关那么在这里设置
ip -6 route add default via "$ipv6_gateway" dev eth0 onlink ip -6 route add default via "$ipv6_gateway" dev eth0 onlink
fi fi
fi fi
}
# 设置静态地址或者设置udhcpc无法设置的网关
add_missing_ipv4_config
add_missing_ipv6_config
# 检查 ipv4/ipv6 是否连接联网 # 检查 ipv4/ipv6 是否连接联网
ipv4_has_internet=false ipv4_has_internet=false
@ -97,9 +108,10 @@ is_need_test_ipv6() {
is_have_ipv6 && ! $ipv6_has_internet is_have_ipv6 && ! $ipv6_has_internet
} }
echo 'Testing Internet Connection...' test_internet() {
echo 'Testing Internet Connection...'
for i in $(seq 5); do for i in $(seq 5); do
{ {
if is_need_test_ipv4 && nslookup www.qq.com $ipv4_dns1; then if is_need_test_ipv4 && nslookup www.qq.com $ipv4_dns1; then
echo "IPv4 has internet." echo "IPv4 has internet."
@ -114,7 +126,23 @@ for i in $(seq 5); do
fi fi
sleep 1 sleep 1
} >/dev/null 2>&1 } >/dev/null 2>&1
done done
}
test_internet
# 处理云电脑 dhcp 获取的地址无法上网
if $dhcpv4 && ! $ipv4_has_internet &&
! [ "$ipv4_addr" = "$(get_first_ipv4_addr)" ]; then
echo "DHCPv4 can't access Internet. And not match static IPv4."
ip -4 addr flush scope global dev eth0
ip -4 route flush dev eth0
add_missing_ipv4_config
test_internet
if $ipv4_has_internet; then
dhcpv4=false
fi
fi
# 等待 udhcpc 创建 /etc/resolv.conf # 等待 udhcpc 创建 /etc/resolv.conf
# 好像只有 dhcpv4 会创建 resolv.conf # 好像只有 dhcpv4 会创建 resolv.conf