core: 通过默认网关得到默认网卡

This commit is contained in:
bin456789 2023-10-22 16:56:44 +08:00
parent ea3dc1ddcf
commit def42d3277
No known key found for this signature in database
GPG Key ID: EE301B386DE6C11B

View File

@ -798,19 +798,29 @@ collect_netconf() {
break break
done done
else else
nic_name=$(ip -o addr show scope global | head -1 | awk '{print $2}') # linux
mac_addr=$(ip addr show scope global | grep link/ether | head -1 | awk '{print $2}') # 通过默认网关得到默认网卡
ipv4_addr=$(ip -4 addr show scope global | grep inet | head -1 | awk '{print $2}') for v in 4 6; do
ipv6_addr=$(ip -6 addr show scope global | grep inet6 | head -1 | awk '{print $2}') if ethx=$(ip -$v route show default | head -1 | awk '{print $5}' | grep .); then
ipv4_gateway=$(ip -4 route show default dev $nic_name | awk '{print $3}') mac_addr=$(ip link show dev $ethx | grep link/ether | head -1 | awk '{print $2}')
ipv6_gateway=$(ip -6 route show default dev $nic_name | awk '{print $3}') break
fi
done
for v in 4 6; do
if ip -$v route show default dev $ethx | head -1 | grep -q .; then
eval ipv${v}_gateway="$(ip -$v route show default dev $ethx | head -1 | awk '{print $3}')"
eval ipv${v}_addr="$(ip -$v -o addr show scope global dev $ethx | head -1 | awk '{print $4}')"
fi
done
fi fi
echo 1 $mac_addr info "Network Info"
echo 2 $ipv4_addr echo "MAC Address: $mac_addr"
echo 3 $ipv4_gateway echo "IPv4 Address: $ipv4_addr"
echo 4 $ipv6_addr echo "IPv4 Gateway: $ipv4_gateway"
echo 5 $ipv6_gateway echo "IPv6 Address: $ipv6_addr"
echo "IPv6 Gateway: $ipv6_gateway"
} }
install_grub_win() { install_grub_win() {