Update build_nat_network.sh

This commit is contained in:
spiritlhl 2023-08-20 21:49:30 +08:00 committed by GitHub
parent c7de6aafab
commit 0b480490b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,7 @@
#!/bin/bash
# from
# https://github.com/spiritLHLS/pve
# 2023.08.17
# 2023.08.20
########## 预设部分输出和部分中间变量
@ -199,11 +199,34 @@ else
fi
${sysctl_path} -p
# 删除可能存在的原有的网卡配置
cp /etc/network/interfaces /etc/network/interfaces_nat.bak
chattr -i /etc/network/interfaces
input_file="/etc/network/interfaces"
output_file="/etc/network/interfaces.tmp"
start_pattern="iface lo inet loopback"
end_pattern="auto vmbr0"
delete_lines=0
while IFS= read -r line; do
if [[ $line == *"$start_pattern"* ]]; then
delete_lines=1
fi
if [ $delete_lines -eq 0 ]; then
echo "$line" >> "$output_file"
fi
if [[ $line == *"$end_pattern"* ]]; then
delete_lines=0
fi
done < "$input_file"
mv "$output_file" "$input_file"
chattr +i /etc/network/interfaces
# 重启配置
service networking restart
systemctl restart networking.service
sleep 3
ifreload -ad
iptables-save | awk '{if($1=="COMMIT"){delete x}}$1=="-A"?!x[$0]++:1' | iptables-restore
# 已加载网络,删除对应缓存文件
if [ -f "/etc/network/interfaces.new" ];then
chattr -i /etc/network/interfaces.new