设置网桥

  • 为拨号网卡创建专属网桥 ```bash

    !/usr/bin/env bash

__create_br() {

  1. # linux 系统网卡名字长度有限制, 如果创建网络接口长度超过15位限制, 那么就会存在不兼容的情况, 需自行解决, 例如 br-if-ens33.1000 这是一个vlan 网桥,长度为16位,超过长度超过4位的网卡名, 如果vlan也有4位以上,那么会出现这个问题
  2. _nic=$1
  3. # 判断网卡是否存在ip,信息, 如果存在则把ip信息配置到网桥中
  4. _gateway=$(ip r | grep "default.*\s${_nic}\s" | head -1 | grep 'via\s[0-9.]{7,15}' -Eo | awk '{print $NF}')
  5. _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}')
  6. _mask=$(ip r | grep -E "/[0-9]{1,3}\sdev\s${_nic}\s" | head -1 | awk -F '/| ' '{print $2}')
  7. if [[ "${_gateway}" != "" ]]; then
  8. 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”

  1. # 创建网卡专属网桥
  2. cat >/etc/sysconfig/network-scripts/ifcfg-"$_bridge" <<AEOF

DEVICE=”${_bridge}” ONBOOT=”yes” TYPE=”Bridge” STP=”off” DELAY=”0” ${_ip_info} AEOF

  1. # 将网卡加入专属网桥
  2. if [[ "$(echo "$_nic" | grep '\.' -Ec)" != "0" ]]; then
  3. _nic2=$(echo "$_nic" | awk -F '.' '{print $1}')
  4. _vlan=$(echo "$_nic" | awk -F '.' '{print $NF}')
  5. _parent_mac=$(cat /sys/class/net/"$_nic2"/address 2>/dev/null)
  6. _macaddr=$(echo "$_parent_mac-$_nic" | md5sum | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/66:\1:\2:\3:\4:\5/')
  7. read -r -d '' _vlan_info <<-EOF

VLAN=”yes” VLAN_ID=${_vlan} MACADDR=${_macaddr} EOF fi

  1. cat >/etc/sysconfig/network-scripts/ifcfg-"$_nic" <<EOL

DEVICE=”${_nic}” BRIDGE=”${_bridge}” ONBOOT=”yes” ${_vlan_info} EOL

  1. # 建议手动重启网卡
  2. # /etc/init.d/network restart

} __create_br p4p1

  1. ```bash
  2. cat >/etc/sysconfig/network-scripts/ifcfg-br-if-p4p1 <<-'AEOF'
  3. DEVICE="br-if-p4p1"
  4. ONBOOT="yes"
  5. TYPE="Bridge"
  6. STP="off"
  7. DELAY="0"
  8. # 网桥上配置 IP
  9. DEFROUTE="no"
  10. BOOTPROTO="static"
  11. GATEWAY="192.168.1.1"
  12. IPADDR="192.168.1.2"
  13. NETMASK="255.255.255.0"
  14. DNS1="223.5.5.5"
  15. DNS2="119.29.29.29"
  16. AEOF

将某一网卡绑定到该网桥

  1. cat >/etc/sysconfig/network-scripts/ifcfg-p4p1 <<-'AEOF'
  2. DEVICE="p4p1" # 当前配置文件网卡名称 也就是 ifcfg-*
  3. BRIDGE="br-if-p4p1" # 将网卡加入哪个网桥
  4. ONBOOT="yes"
  5. AEOF

vlan 网卡vlan 加入网桥

  1. cat >/etc/sysconfig/network-scripts/ifcfg-p4p1.99 <<-'AEOF'
  2. BRIDGE="br-ikuai-lan2"
  3. NAME=p4p1.99
  4. DEVICE=p4p1.99
  5. ONBOOT="yes"
  6. VLAN="yes"
  7. VLAN_ID=99
  8. AEOF