设置网桥
__create_br() {
# linux 系统网卡名字长度有限制, 如果创建网络接口长度超过15位限制, 那么就会存在不兼容的情况, 需自行解决, 例如 br-if-ens33.1000 这是一个vlan 网桥,长度为16位,超过长度超过4位的网卡名, 如果vlan也有4位以上,那么会出现这个问题
_nic=$1
# 判断网卡是否存在ip,信息, 如果存在则把ip信息配置到网桥中
_gateway=$(ip r | grep "default.*\s${_nic}\s" | head -1 | grep 'via\s[0-9.]{7,15}' -Eo | awk '{print $NF}')
_ip=$(ip r | grep -E "/[0-9]{1,3}\sdev\s${_nic}\s" | head -1 | grep -Eo 'src\s[0-9.]{7,15}' | awk '{print $NF}')
_mask=$(ip r | grep -E "/[0-9]{1,3}\sdev\s${_nic}\s" | head -1 | awk -F '/| ' '{print $2}')
if [[ "${_gateway}" != "" ]]; then
read -r -d '' _ip_info <<-EOF
DEFROUTE=”yes” IPADDR=”${_ip}” PREFIX=”${_mask}” GATEWAY=”${_gateway}” DNS1=”223.5.5.5” DNS2=”119.29.29.29” EOF fi _bridge=”br-if-$_nic”
# 创建网卡专属网桥
cat >/etc/sysconfig/network-scripts/ifcfg-"$_bridge" <<AEOF
DEVICE=”${_bridge}” ONBOOT=”yes” TYPE=”Bridge” STP=”off” DELAY=”0” ${_ip_info} AEOF
# 将网卡加入专属网桥
if [[ "$(echo "$_nic" | grep '\.' -Ec)" != "0" ]]; then
_nic2=$(echo "$_nic" | awk -F '.' '{print $1}')
_vlan=$(echo "$_nic" | awk -F '.' '{print $NF}')
_parent_mac=$(cat /sys/class/net/"$_nic2"/address 2>/dev/null)
_macaddr=$(echo "$_parent_mac-$_nic" | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/66:\1:\2:\3:\4:\5/')
read -r -d '' _vlan_info <<-EOF
VLAN=”yes” VLAN_ID=${_vlan} MACADDR=${_macaddr} EOF fi
cat >/etc/sysconfig/network-scripts/ifcfg-"$_nic" <<EOL
DEVICE=”${_nic}” BRIDGE=”${_bridge}” ONBOOT=”yes” ${_vlan_info} EOL
# 建议手动重启网卡
# /etc/init.d/network restart
} __create_br p4p1
```bash
cat >/etc/sysconfig/network-scripts/ifcfg-br-if-p4p1 <<-'AEOF'
DEVICE="br-if-p4p1"
ONBOOT="yes"
TYPE="Bridge"
STP="off"
DELAY="0"
# 网桥上配置 IP
DEFROUTE="no"
BOOTPROTO="static"
GATEWAY="192.168.1.1"
IPADDR="192.168.1.2"
NETMASK="255.255.255.0"
DNS1="223.5.5.5"
DNS2="119.29.29.29"
AEOF
将某一网卡绑定到该网桥
cat >/etc/sysconfig/network-scripts/ifcfg-p4p1 <<-'AEOF'
DEVICE="p4p1" # 当前配置文件网卡名称 也就是 ifcfg-*
BRIDGE="br-if-p4p1" # 将网卡加入哪个网桥
ONBOOT="yes"
AEOF
vlan 网卡vlan 加入网桥
cat >/etc/sysconfig/network-scripts/ifcfg-p4p1.99 <<-'AEOF'
BRIDGE="br-ikuai-lan2"
NAME=p4p1.99
DEVICE=p4p1.99
ONBOOT="yes"
VLAN="yes"
VLAN_ID=99
AEOF