From 0f658612703d65d75b8dcb474ccfe0409d1cbe78 Mon Sep 17 00:00:00 2001 From: spiritLHLS <103393591+spiritLHLS@users.noreply.github.com> Date: Sun, 23 Apr 2023 22:29:54 +0800 Subject: [PATCH] Update build_nat_network.sh --- scripts/build_nat_network.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/scripts/build_nat_network.sh b/scripts/build_nat_network.sh index 19c8c6f..6046555 100644 --- a/scripts/build_nat_network.sh +++ b/scripts/build_nat_network.sh @@ -69,6 +69,30 @@ else fi ${sysctl_path} -p +# 检查/etc/network/interfaces是否有source /etc/network/interfaces.d/*行 +if grep -q "source /etc/network/interfaces.d/*" /etc/network/interfaces; then + # 检查/etc/network/interfaces.d/文件夹下是否有50-cloud-init文件 + if [ -f /etc/network/interfaces.d/50-cloud-init ]; then + # 检查50-cloud-init文件中是否有iface eth0 inet dhcp行 + if grep -q "iface eth0 inet dhcp" /etc/network/interfaces.d/50-cloud-init; then + # 获取ipv4、subnet、gateway信息 + gateway=$(ip route | awk '/default/ {print $3}') + eth0info=$(ip -o -4 addr show dev eth0 | awk '{print $4}') + ipv4=$(echo $eth0info | cut -d'/' -f1) + subnet=$(echo $eth0info | cut -d'/' -f2) + subnet=$(ipcalc -n "$ipv4/$subnet" | grep -oP 'Netmask:\s+\K.*' | awk '{print $1}') + chattr -i /etc/network/interfaces.d/50-cloud-init + sed -i "/iface eth0 inet dhcp/c\ + iface eth0 inet static\n\ + address $ipv4\n\ + netmask $subnet\n\ + gateway $gateway\n\ + dns-nameservers 8.8.8.8 8.8.4.4" /etc/network/interfaces.d/50-cloud-init + chattr +i /etc/network/interfaces.d/50-cloud-init + fi + fi +fi + # 重启配置 service networking restart systemctl restart networking.service