diff --git a/network.sh b/network.sh index ab38b5f..0a119da 100644 --- a/network.sh +++ b/network.sh @@ -4,9 +4,20 @@ # 设置虚拟网桥名称 BRIDGE_NAME="vmbr0" -# 获取本地IPv4地址 子网掩码 网关地址 +# 获取本地IPv4地址和子网掩码 IP_ADDR=$(ip route get 8.8.8.8 | head -1 | awk '{print $7}') -SUBNET_MASK="255.255.255.0" +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 + NET_ADDR="${NET_ADDR}$((${IP_ARR[$i]} & ${MASK_ARR[$i]}))." +done +NET_ADDR=${NET_ADDR::-1} + +# 获取网关地址 GATEWAY=$(ip route | awk '/default/ {print $3}') # 创建虚拟网桥 @@ -17,7 +28,7 @@ iface $BRIDGE_NAME inet static address $IP_ADDR netmask $SUBNET_MASK gateway $GATEWAY - bridge_ports none + bridge_ports ens18 bridge_vlan_aware 1 vlan_ids 100 EOF