pve/build_nat_network.sh

52 lines
1.4 KiB
Bash
Raw Normal View History

2023-02-23 21:42:24 +08:00
#!/bin/bash
2023-02-24 09:56:53 +08:00
#from https://github.com/spiritLHLS/pve
2023-02-23 21:58:44 +08:00
2023-02-24 14:28:18 +08:00
# 打印信息
_red() { echo -e "\033[31m\033[01m$@\033[0m"; }
_green() { echo -e "\033[32m\033[01m$@\033[0m"; }
_yellow() { echo -e "\033[33m\033[01m$@\033[0m"; }
_blue() { echo -e "\033[36m\033[01m$@\033[0m"; }
# 查询信息
2023-02-24 09:56:53 +08:00
interface=$(lshw -C network | awk '/logical name:/{print $3}' | head -1)
2023-02-24 13:44:53 +08:00
ip=$(curl -s ipv4.ip.sb)/24
2023-02-24 09:56:53 +08:00
gateway=$(ip route | awk '/default/ {print $3}')
2023-02-24 14:28:18 +08:00
# 录入网关
2023-02-24 13:50:58 +08:00
if grep -q "vmbr0" /etc/network/interfaces; then
2023-02-24 14:28:18 +08:00
echo "vmbr0 已存在在 /etc/network/interfaces"
2023-02-24 13:50:58 +08:00
else
2023-02-24 13:53:12 +08:00
cat << EOF | sudo tee -a /etc/network/interfaces
auto vmbr0
iface vmbr0 inet static
address $ip
gateway $gateway
bridge_ports $interface
bridge_stp off
bridge_fd 0
EOF
2023-02-24 13:50:58 +08:00
fi
if grep -q "vmbr1" /etc/network/interfaces; then
2023-02-24 14:28:18 +08:00
echo "vmbr1 已存在在 /etc/network/interfaces"
2023-02-24 13:50:58 +08:00
else
2023-02-24 13:53:12 +08:00
cat << EOF | sudo tee -a /etc/network/interfaces
auto vmbr1
iface vmbr1 inet static
address 172.16.1.1
netmask 255.255.255.0
bridge_ports none
bridge_stp off
bridge_fd 0
post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up echo 1 > /proc/sys/net/ipv4/conf/vmbr1/proxy_arp
post-up iptables -t nat -A POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE
EOF
2023-02-24 13:50:58 +08:00
fi
2023-02-24 09:56:53 +08:00
2023-02-24 14:28:18 +08:00
# 重启配置
2023-02-24 09:56:53 +08:00
service networking restart
2023-02-23 22:03:19 +08:00
systemctl restart networking.service
2023-02-24 14:28:18 +08:00
# 配置DHCP