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'
fi
get_first_ipv4_addr() {
ip -4 -o addr show scope global dev eth0 | head -1 | awk '{print $4}'
}
is_have_ipv4_addr() {
ip -4 addr show scope global dev eth0 | grep -q inet
}
@ -62,7 +66,7 @@ done
is_have_ipv4_addr && dhcpv4=true || dhcpv4=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 ! is_have_ipv4_addr; then
ip -4 addr add "$ipv4_addr" dev eth0
@ -73,7 +77,9 @@ if [ -n "$ipv4_addr" ] && [ -n "$ipv4_gateway" ]; then
ip -4 route add default via "$ipv4_gateway" dev eth0 onlink
fi
fi
}
add_missing_ipv6_config() {
if [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then
if ! is_have_ipv6_addr; then
ip -6 addr add "$ipv6_addr" dev eth0
@ -84,6 +90,11 @@ if [ -n "$ipv6_addr" ] && [ -n "$ipv6_gateway" ]; then
ip -6 route add default via "$ipv6_gateway" dev eth0 onlink
fi
fi
}
# 设置静态地址或者设置udhcpc无法设置的网关
add_missing_ipv4_config
add_missing_ipv6_config
# 检查 ipv4/ipv6 是否连接联网
ipv4_has_internet=false
@ -97,6 +108,7 @@ is_need_test_ipv6() {
is_have_ipv6 && ! $ipv6_has_internet
}
test_internet() {
echo 'Testing Internet Connection...'
for i in $(seq 5); do
@ -115,6 +127,22 @@ for i in $(seq 5); do
sleep 1
} >/dev/null 2>&1
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
# 好像只有 dhcpv4 会创建 resolv.conf