Update network.sh

This commit is contained in:
spiritLHLS 2023-02-24 13:50:58 +08:00 committed by GitHub
parent f7f6b8b7ff
commit ac2998f395
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,29 +4,39 @@
interface=$(lshw -C network | awk '/logical name:/{print $3}' | head -1) interface=$(lshw -C network | awk '/logical name:/{print $3}' | head -1)
ip=$(curl -s ipv4.ip.sb)/24 ip=$(curl -s ipv4.ip.sb)/24
gateway=$(ip route | awk '/default/ {print $3}') gateway=$(ip route | awk '/default/ {print $3}')
cat << EOF | sudo tee -a /etc/network/interfaces if grep -q "vmbr0" /etc/network/interfaces; then
auto vmbr0 echo "vmbr0 already exists in /etc/network/interfaces"
iface vmbr0 inet static else
address $ip # Add the vmbr0 configuration block
gateway $gateway cat << EOF | sudo tee -a /etc/network/interfaces
bridge_ports $interface auto vmbr0
bridge_stp off iface vmbr0 inet static
bridge_fd 0 address $ip
EOF gateway $gateway
bridge_ports $interface
bridge_stp off
bridge_fd 0
EOF
fi
cat << EOF | sudo tee -a /etc/network/interfaces if grep -q "vmbr1" /etc/network/interfaces; then
auto vmbr1 echo "vmbr1 already exists in /etc/network/interfaces"
iface vmbr1 inet static else
address 172.16.1.1 # Add the vmbr1 configuration block
netmask 255.255.255.0 cat << EOF | sudo tee -a /etc/network/interfaces
bridge_ports none auto vmbr1
bridge_stp off iface vmbr1 inet static
bridge_fd 0 address 172.16.1.1
post-up echo 1 > /proc/sys/net/ipv4/ip_forward netmask 255.255.255.0
post-up echo 1 > /proc/sys/net/ipv4/conf/vmbr1/proxy_arp bridge_ports none
post-up iptables -t nat -A POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE bridge_stp off
post-down iptables -t nat -D POSTROUTING -s '172.16.1.0/24' -o vmbr0 -j MASQUERADE bridge_fd 0
EOF 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
fi
service networking restart service networking restart
systemctl restart networking.service systemctl restart networking.service