pve/network.sh

36 lines
1.4 KiB
Bash
Raw Normal View History

2023-02-23 21:42:24 +08:00
#!/bin/bash
2023-02-23 21:58:44 +08:00
# 该脚本将创建一个名为vmbr0的虚拟网桥并将其记录到配置文件中。它会自动获取本地IPV4地址、子网掩码和网关并将其添加到vmbr0配置中。
2023-02-23 21:42:24 +08:00
# 设置虚拟网桥名称
BRIDGE_NAME="vmbr0"
# 获取本地IPv4地址和子网掩码
IP_ADDR=$(ip route get 8.8.8.8 | head -1 | awk '{print $7}')
SUBNET_MASK=$(ip route get 8.8.8.8 | head -1 | awk '{print $1}' | awk -F"/" '{print $2}')
# 计算网络地址
IFS='.' read -ra IP_ARR <<< "$IP_ADDR"
IFS='.' read -ra MASK_ARR <<< "$SUBNET_MASK"
NET_ADDR=""
for ((i=0; i<4; i++)); do
2023-02-23 21:47:22 +08:00
NET_ADDR="${NET_ADDR}$((${IP_ARR[$i]} & ${MASK_ARR[$i]}))."
2023-02-23 21:42:24 +08:00
done
NET_ADDR=${NET_ADDR::-1}
# 获取网关地址
GATEWAY=$(ip route | awk '/default/ {print $3}')
2023-02-23 21:58:44 +08:00
# 创建虚拟网桥的配置文件
echo "# This file is generated by create_vmbr0.sh script" > /tmp/$BRIDGE_NAME.conf
echo "auto $BRIDGE_NAME" >> /tmp/$BRIDGE_NAME.conf
echo "iface $BRIDGE_NAME inet static" >> /tmp/$BRIDGE_NAME.conf
echo " address $IP_ADDR" >> /tmp/$BRIDGE_NAME.conf
echo " netmask $SUBNET_MASK" >> /tmp/$BRIDGE_NAME.conf
echo " gateway $GATEWAY" >> /tmp/$BRIDGE_NAME.conf
echo " bridge_ports none" >> /tmp/$BRIDGE_NAME.conf
echo " bridge_vlan_aware 1" >> /tmp/$BRIDGE_NAME.conf
echo " vlan_ids 100" >> /tmp/$BRIDGE_NAME.conf
# 将配置文件移动到网络接口配置目录
mv /tmp/$BRIDGE_NAME.conf /etc/network/interfaces.d/$BRIDGE_NAME.conf