Linux双网卡绑定脚本的方法示例

linux运维及配置工作中,常常会用到双网卡绑定,少数几台服务器的配置还好,如果是需要配置几十甚至上百台,难免会枯燥乏味,易于出错,我编写了这个双网卡绑定的辅助脚本,可傻瓜式地完成linux双网卡绑定工作,当然,该脚本主要还是用于小批量的系统配置,如需配置大量的服务器,可提取脚本中的bonding函数,稍作修改即可,你值得一试!

1.适用范围

该shell脚本可在以下linux系统创建多个绑定网卡,用于生产环境没问题的:

  • Redhat 5.x
  • CentOS 5.x
  • Kylin 3.x
  • KUX 2.x

2.脚本特点

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:

First NIC   :eth0
Second NIC  :eth1
bond name   :bond1
IP address  :192.168.56.11
netmask    :255.255.255.0
bonding_mode :mode=1
primary NIC  :none
----------------------------------------------

Pls make sure its OK[y/n]:y

如上,脚本在使用过程中,会依次提示输入需要绑定的子网卡名称、绑定网卡名称、IP地址和子网掩码、绑定模式、是否指定主网卡,并主动对系统中可用网卡进行识别并列出,供用户选择,同时会判断输入的各项信息的正确性,保障双网卡绑定顺利进行。

3.使用说明

首先将脚本上传至linux系统任意目录下,使用root用户执行:

[root@node1 tmp]# sh bonding_v2.0.sh 

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

4 network cards available:
eth0 eth1 eth2 eth3
----------------------------------------------

Please enter the First NIC:_

首先脚本会列出系统中有多少可用网卡,并提示输入需要绑定的第一块子网卡的名称,这里输入eth2。

无需担心输入错误,输入重复等问题,脚本会自动判断你的输入信息是否正确。

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

4 network cards available:
eth0 eth1 eth2 eth3

The information you have entered:
First NIC   : eth2
----------------------------------------------

Please select the second NIC:_

接着输入需要绑定的第二块网卡的名称eth3,如果输入错误会出现相应的提示,比如错输入为eth8,会提示需要重新输入:

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

4 network cards available:
eth0 eth1 eth2 eth3

The information you have entered:
First NIC   : eth2
----------------------------------------------

eth8 is not available,Please enter another one.

Please select the second NIC:_

正确输入需要绑定的第二块网卡eth3后,如果linux系统中之前不存在绑定网卡,则默认第一个绑定网卡的名称为bond0,如果系统中存在绑定网卡,脚本会提示输入bondN(N为数字):

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:
First NIC   : eth2
Second NIC  : eth3
bond name   : bond0
----------------------------------------------

The default first bond name is -- bond0.

Please enter an IP address:_

按照提示输入需要绑定的IP地址,比如10.1.1.1

脚本会自动判断输入的IP地址格式和范围是否正确,无需担心输入错误

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:
First NIC   : eth2
Second NIC  : eth3
bond name   : bond0
IP address  : 10.1.1.1
----------------------------------------------

Pls enter the NETMASK[255.255.255.0]:_

输入IP地址后,会提示输入子网掩码,默认为255.255.255.0,直接回车即可,也可以输入自定义的子网掩码。

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:
First NIC   : eth2
Second NIC  : eth3
bond name   : bond0
IP address  : 10.1.1.1
netmask    : 255.255.255.0
----------------------------------------------

Pls enter the bonding_mode[default:mode=1,active-backup]:_

这一步是输入网卡绑定级别,默认使用主备模式:mode=1,直接回车即可,也可以输入自定义的绑定级别(如输入 mode=6),格式为:

mode={0,1,2,3,4,5,6}

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:

First NIC   :eth2
Second NIC  :eth3
bond name   :bond0
IP address  :10.1.1.1
netmask    :255.255.255.0
bonding_mode :mode=1
----------------------------------------------

1-eth2
2-eth3
3-none
choose whether to set up the primary NIC or not[default:3-none]:_

此时脚本提示是否设置primary网卡,primary网卡的作用是在子网卡均正常时,primary网卡优先处于活动状态,这里默认为不设置primary网卡,也可以输入数字1或2选择一个网卡为primary网卡。

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

The information you have entered:

First NIC   :eth2
Second NIC  :eth3
bond name   :bond0
IP address  :10.1.1.1
netmask    :255.255.255.0
bonding_mode :mode=1
primary NIC  :none
----------------------------------------------

Pls make sure its OK[y/n]:_

至此,所有信息输入完毕,脚本会让您判断输入的所有信息是否正确,正确则输入y,进行下一步操作,否则输入n,重新输入以上信息。

----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

Complete!
File backup directory: /etc/sysconfig/network-scripts/inspur_bak

you can check the file and then restart the network service.

1 ) service network restart
2 ) exit
----------------------------------------------
Please make your choice:_

看到这里时,所有绑定网卡的配置文件已经修改完毕了,输入1重启网络服务;输入2直接退出脚本。

建议选择重启网络服务,如果直接退出脚本而不重启网络服务,修改的配置文件是不会生效的,并且在使用该脚本创建下一个绑定网卡时显示信息会出现异常(因为脚本通过ifconfig命令获取当前网卡信息)。

you can check the file and then restart the network service.

1 ) service network restart
2 ) exit
  ----------------------------------------------
Please make your choice:1
正在关闭接口 eth0:                    [确定]
正在关闭接口 eth1:                    [确定]
正在关闭接口 eth2:                    [确定]
正在关闭接口 eth3:                    [确定]
关闭环回接口:                       [确定]
弹出环回接口:                       [确定]
弹出界面 bond0: RTNETLINK answers: File exists
在 bond0 添加地址 10.1.1.1 时出错。
                              [确定]
弹出界面 eth0:                      [确定]
弹出界面 eth1:                      [确定]

第一次重启网络服务时可能会出现以上类似报错,无需担心,其实配置已经完成,再次重启网络服务即恢复正常。

在绑定过程中修改的配置文件如下:

[root@node1 tmp]# cd /etc/sysconfig/network-scripts/
[root@node1 network-scripts]# cat ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
IPADDR=10.1.1.1
NETMASK=255.255.255.0
BONDING_OPTS="miimon=100 mode=1"

[root@node1 network-scripts]# cat ifcfg-eth2
DEVICE=eth2
BOOTPROTO=none
#HWADDR=08:00:27:41:b4:16
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
SLAVE=yes
MASTER=bond0

[root@node1 network-scripts]# cat ifcfg-eth3
DEVICE=eth3
BOOTPROTO=none
#HWADDR=08:00:27:6c:3d:0c
ONBOOT=yes
TYPE=Ethernet
USERCTL=no
IPV6INIT=no
PEERDNS=yes
SLAVE=yes
MASTER=bond0

[root@node1 network-scripts]# cat /etc/modprobe.conf
alias scsi_hostadapter ata_piix
alias scsi_hostadapter1 ahci
alias net-pf-10 off
alias ipv6 off
options ipv6 disable=1
alias snd-card-0 snd-intel8x0
options snd-card-0 index=0
options snd-intel8x0 index=0
remove snd-intel8x0 { /usr/sbin/alsactl store 0 >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove snd-intel8x0
alias eth0 e1000
alias eth1 e1000
alias eth2 e1000
alias eth3 e1000
alias bond0 bonding

网络配置信息如下:

[root@node1 tmp]# ifconfig
bond0   Link encap:Ethernet HWaddr 08:00:27:41:B4:16
     inet addr:10.1.1.1 Bcast:10.1.1.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
     RX packets:1197 errors:0 dropped:0 overruns:0 frame:0
     TX packets:125 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0
     RX bytes:106338 (103.8 KiB) TX bytes:16662 (16.2 KiB)

eth0   Link encap:Ethernet HWaddr 08:00:27:35:B4:81
     inet addr:192.168.56.11 Bcast:192.168.56.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:1487 errors:0 dropped:0 overruns:0 frame:0
     TX packets:1190 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:146576 (143.1 KiB) TX bytes:151787 (148.2 KiB)

eth1   Link encap:Ethernet HWaddr 08:00:27:72:08:46
     inet addr:10.1.1.11 Bcast:10.1.1.255 Mask:255.255.255.0
     UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
     RX packets:193 errors:0 dropped:0 overruns:0 frame:0
     TX packets:455 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:31434 (30.6 KiB) TX bytes:22657 (22.1 KiB)

eth2   Link encap:Ethernet HWaddr 08:00:27:41:B4:16
     UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
     RX packets:591 errors:0 dropped:0 overruns:0 frame:0
     TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:55217 (53.9 KiB) TX bytes:10511 (10.2 KiB)

eth3   Link encap:Ethernet HWaddr 08:00:27:41:B4:16
     UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
     RX packets:606 errors:0 dropped:0 overruns:0 frame:0
     TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:1000
     RX bytes:51121 (49.9 KiB) TX bytes:6151 (6.0 KiB)

lo    Link encap:Local Loopback
     inet addr:127.0.0.1 Mask:255.0.0.0
     UP LOOPBACK RUNNING MTU:16436 Metric:1
     RX packets:3297 errors:0 dropped:0 overruns:0 frame:0
     TX packets:3297 errors:0 dropped:0 overruns:0 carrier:0
     collisions:0 txqueuelen:0
     RX bytes:4725726 (4.5 MiB) TX bytes:4725726 (4.5 MiB)

双网卡绑定信息:

[root@node1 tmp]# cat /proc/net/bonding/bond0
Ethernet Channel Bonding Driver: v3.4.0-2 (October 7, 2008)

Bonding Mode: fault-tolerance (active-backup)
Primary Slave: None
Currently Active Slave: eth2
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth2
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 08:00:27:41:b4:16

Slave Interface: eth3
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 08:00:27:6c:3d:0c

4.脚本代码

#!/bin/bash
# Date:2018-11-26 14:28:45
# Author:ZhangJian
# Mail:1037509307@qq.com
# Func:Configure Network Bonding
# Version:2.0
#V1.6版本更新:不再将绑定的模式mode=x写入modprobe.conf文件,将其写入ifcfg-bond中,因为在modprobe.conf文件中时,修改了mode模式后,重启网络服务可能不生效。
#V2.0版本更新:将primary网卡设置为可选择项。

trap "tput clear;tput cup 3;echo 'Any Questions: Send a message to QQ 1037509307.';tput cup 6;exit" 2 3
if [[ -n $1 ]]; then
 cat <<EOF

Network Configuration Assistant
          --- Configure Network Bonding v1.5

Usage:

    sh $0
    chmod +x $0 && ./$0

EOF
exit 0
fi

if [[ $UID -ne 0 ]]; then
 tput clear
 tput cup 6 20
 echo -e "You must use the user: \033[31mROOT\033[0m"
 tput cup 10
 exit
fi

ERROR(){
tput cup $1 $2;tput ed
echo 'Input error,Try again pls.'
echo -e 'Press ENTER to continue..._\b\c'
read inputA
}

Check_BakFile(){
#文件备份函数
#使用方法:
#Check_BakFile 要备份的文件名 备份目录 -x(按什么时间格式备份)
case ${3} in
  -d ) #按天备份
    Bak_Date=`date '+%Y-%m-%d'`
    ;;
  -H ) #按小时备份
    Bak_Date=`date '+%Y-%m-%d_%H'`
    ;;
  -M ) #按分钟备份
    Bak_Date=`date '+%Y-%m-%d_%H:%M'`
    ;;
  -m ) #按月备份
    Bak_Date=`date '+%Y-%m'`
    ;;
  -Y ) #按年备份
    Bak_Date=`date '+%Y'`
    ;;
  * ) #默认按分钟备份
    Bak_Date=`date '+%Y-%m-%d_%H:%M'`
    ;;
esac
#Bak_Date=`date '+%Y-%m-%d-%H:%M'`

[[ -d ${2}/${Bak_Date} ]] || mkdir -p ${2}/${Bak_Date}
cp -ra ${1} ${2}/${Bak_Date}
}

bonding_pre(){
#双网卡绑定前导函数,判定网卡、IP等合法性并引导用户正确输入参数。
#全部真实网卡
NIC_NAME_all=`ifconfig -a | awk '/\<Ethernet\>/ {print $1}' | grep -wEv '^bond[0-9]+'`
#已经存在的绑定网卡
NIC_NAME_bond=`ifconfig -a | awk '/^\<bond[0-9]+\>/ {print $1}'`

#打印出已经是SLAVE的网卡
NIC_NAME_slave=`ifconfig -a | sed -n '/SLAVE/{g;1!p;};h' | awk '{print $1}'`
NIC_NAME_slave=${NIC_NAME_slave:=NULL}
#打印出可用网卡
NIC_NAME_free=`echo "$NIC_NAME_all" | grep -Fwv "$NIC_NAME_slave"`
#可用网卡数量
NIC_NAME_free_nu=`echo "$NIC_NAME_free" | wc -w`

declare -a NIC_LIST
NIC_LIST=($NIC_NAME_free)

info_print(){
tput clear;tput cup 2
cat <<EOF
----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------
EOF
tput cup 7
if [[ -n ${1} ]]; then
 echo -e "Already existing Channel Bonding Interface of the system:\n\033[31m${1}\033[0m\n"
fi
}

if [[ "$NIC_NAME_free_nu" -gt 1 ]]; then
 info_print
 echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:"
 echo -e "\033[31m${NIC_LIST[@]}\033[0m"
 echo -e "----------------------------------------------\n"
 #输入第一块网卡的名称
 while true; do
  echo -e "\nPlease enter the First NIC:_\b\c"
  read NIC1
  echo ${NIC_LIST[@]} | grep -Fw "$NIC1" &> /dev/null
  if [[ $? -eq 0 ]]; then
   info_print
   echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:\n\033[31m${NIC_LIST[@]}\033[0m"
   echo -e "\nThe information you have entered:\nFirst NIC   : $NIC1"
   echo -e "----------------------------------------------\n"
   break
  else
   info_print
   echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:\n\033[31m${NIC_LIST[@]}\033[0m"
   echo -e "----------------------------------------------\n"
   echo -e "\033[31m${NIC1}\033[0m is not available,Please enter another one."
  fi
 done
 #输入第二块网卡的名称
 while true; do
  echo -e "\nPlease select the second NIC:_\b\c"
  read NIC2
  if [[ ${NIC1} != ${NIC2} ]]; then
   echo ${NIC_LIST[@]} | grep -Fw "${NIC2}" &> /dev/null
   if [[ $? -eq 0 ]]; then
    info_print "${NIC_NAME_bond}"
    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}"
    echo -e "----------------------------------------------\n"
    break
   else
    info_print
    echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:\n\033[31m${NIC_LIST[@]}\033[0m\n\nThe information you have entered:\nFirst NIC   : ${NIC1}"
    echo -e "----------------------------------------------\n"
    echo -e "\033[31m${NIC2}\033[0m is not available,Please enter another one."
   fi
  else
   info_print
   echo -e "\033[031m${NIC_NAME_free_nu}\033[0m network cards available:\n\033[31m${NIC_LIST[@]}\033[0m\n\nThe information you have entered:\nFirst NIC   : ${NIC1}"
   echo -e "----------------------------------------------\n"
   echo -e "\033[31m${NIC2}\033[0m is the first NIC,Please enter another one."
  fi
 done

 #输入绑定网卡名称并进行合理性检查
 if [[ -z ${NIC_NAME_bond} ]]; then
  NAME_bond=bond0
  info_print
  echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}"
  echo -e "----------------------------------------------\n"
  echo "The default first bond name is -- bond0."
 else
  while true; do
   echo -e "\nPlease enter a bond name[bonN]:_\b\c"
   read NAME_bond
   #检查输入格式是否为bond+数字的格式。
   echo "${NAME_bond}" | grep -wE '^bond[[:digit:]]+$' &> /dev/null
   if [[ $? -eq 0 ]]; then
    echo "${NIC_NAME_bond}" | grep -Fw "${NAME_bond}" &> /dev/null
    if [[ $? -ne 0 ]]; then
     info_print "${NIC_NAME_bond}"
     echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}"
     echo -e "----------------------------------------------\n"
     break
    else
     info_print "${NIC_NAME_bond}"
     echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}"
     echo -e "----------------------------------------------\n"
     echo "You can not use an existing name: ${NAME_bond}"
     echo -e "\033[31m${NAME_bond}\033[0m is not available,Please enter another one like -- bondN."
    fi
   else
    info_print "${NIC_NAME_bond}"
    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}"
    echo -e "----------------------------------------------\n"
    echo -e "\033[31m${NAME_bond}\033[0m is not available,Please enter another one like -- bondN."
   fi
  done
 fi

 #设置IP地址
 while true; do
  echo -e "\nPlease enter an IP address:_\b\c"
  read IP_bond
  echo "${IP_bond}" | grep -owE '^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})\.){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-5]{2})$' &> /dev/null
  if [[ $? -eq 0 ]]; then
   info_print
   echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}"
   echo -e "----------------------------------------------\n"
   break
  else
   info_print
   echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}"
   echo -e "----------------------------------------------\n"
   echo -e "IP address: \033[31m${IP_bond}\033[0m format errors, please re-enter."
  fi
 done
 #设置netmask
 while true; do
  echo -e "\nPls enter the NETMASK[255.255.255.0]:_\b\c"
  read NETMASK_bond
  NETMASK_bond=${NETMASK_bond:="255.255.255.0"}  #当变量为NETMASK_bond为空时,给其赋默认值255.255.255.0
  echo "$NETMASK_bond" | grep -owE '^(128|192|224|240|248|252|254|255)\.((0|128|192|224|240|248|252|254|255)\.){2}(0|128|192|224|240|248|252|254|255)$' &> /dev/null
  if [[ $? -eq 0 ]]; then
   info_print
   echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}\nnetmask    : ${NETMASK_bond}"
   echo -e "----------------------------------------------\n"
   break
  else
   info_print
   echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}"
   echo -e "----------------------------------------------\n"
   echo -e "Input error.\nPlease enter the correct NETMASK or press ENTER to use 255.255.255.0.\n"
  fi
 done
#:<<!zhushi! #批量注释,如果开启批量注释,默认mode=1
 while true; do
  echo -e "\nPls enter the bonding_mode[default:mode=1,active-backup]:_\b\c"
  read mode_bond
  mode_bond=${mode_bond:="mode=1"}  #当变量为mode_bond为空时,给其赋默认值1
  if [[ ! ${mode_bond} =~ "mode=[0-6]" ]]; then
   info_print
   echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}\netmask    : ${NETMASK_bond}"
   echo -e "----------------------------------------------\n"
   echo "Invalid answer: ${mode_bond}"
   echo "Eg: mode={0,1,2,3,4,5,6}"
  else
   info_print
   echo -e "The information you have entered:\n"
   echo -e "First NIC   :${NIC1}\nSecond NIC  :${NIC2}\nbond name   :${NAME_bond}\nIP address  :${IP_bond}\nnetmask    :${NETMASK_bond}\nbonding_mode :${mode_bond}"
   echo -e "----------------------------------------------\n"
   break
  fi
 done
#!zhushi!

 #选择是否设置primary网卡
 while true; do
  echo "1-${NIC1}"
  echo "2-${NIC2}"
  echo "3-none"
  echo -e "choose whether to set up the primary NIC or not[default:3-none]:_\b\c"
  read get_primary_NIC
  get_primary_NIC=${get_primary_NIC:="none"}
    case ${get_primary_NIC} in
    1 )
     get_primary_NIC=${NIC1}
     #echo "--$get_primary_NIC"
     break
     ;;
    2 )
     get_primary_NIC=${NIC2}
      #echo "--$get_primary_NIC"
     break
     ;;
    3 )
     get_primary_NIC=none
      #echo "--$get_primary_NIC"
     break
     ;;
    none )
     break
     ;;
    * )
      #echo "--$get_primary_NIC"
     info_print
     echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}\nnetmask    : ${NETMASK_bond}"
     echo -e "----------------------------------------------\n"
     echo "Invalid answer: ${get_primary_NIC}"
     echo "Pls input a number in {1,2,3} or press ENTER to set up primary NIC none."
     continue
     ;;
   esac

   info_print
   echo -e "The information you have entered:\n"
   echo -e "First NIC   :${NIC1}\nSecond NIC  :${NIC2}\nbond name   :${NAME_bond}\nIP address  :${IP_bond}\nnetmask    :${NETMASK_bond}\nbonding_mode :${mode_bond}\nprimary NIC  :${get_primary_NIC}"
   echo -e "----------------------------------------------\n"
   break
 done

 #最终输入信息确认
 while true; do
  info_print
  echo -e "The information you have entered:\n"
  echo -e "\033[31mFirst NIC   :${NIC1}\nSecond NIC  :${NIC2}\nbond name   :${NAME_bond}\nIP address  :${IP_bond}\nnetmask    :${NETMASK_bond}\nbonding_mode :${mode_bond}\nprimary NIC  :${get_primary_NIC}\033[0m"
  echo -e "----------------------------------------------\n"
  echo -e "Pls make sure its OK[y/n]:_\b\c"
  read input
  case ${input} in
   [Yy]|[Yy][Ee][Ss] )
    bonding "$NIC1" "$NIC2" "$NAME_bond" "$IP_bond" "$NETMASK_bond"
    break
    ;;
   [Nn]|[Nn][Oo] )
    bonding_pre
   ;;
   * )
    tput cup 7;tput ed
    ERROR 7 10
   ;;
  esac
 done
else
  info_print
  echo -e "You have \033[031m${NIC_NAME_free_nu}\033[0m network cards available:\n"
  #将重点显示的“网卡名”标示为红色
  echo -e "\033[31m${NIC_LIST[@]}\033[0m\n"
  echo -e "There are \033[31mnot enough\033[0m network cards to make bonding"
  echo -e "Pls check it......\n"
  echo -e 'Press ENTER to exit..._\b\c'
  read answer
  exit 1
fi
}

bonding(){
if [[ $# -lt 5 ]]
then
 echo 'Bonding failed! Please provide enough information!'
 echo -e "\nUsage:\n    sh bonding.sh <NIC1_name> <NIC2_name> <bond_name> <IP> <NETMASK>\n\n"
 exit 1
fi
#get device name and ip information
SLAVE1_DEV="$1"    #SLAVE1_DEV=ethx
SLAVE2_DEV="$2"    #SLAVE2_DEV=ethx
BOND_DEV="$3"     #BOND_DEV=bondx
SLAVE1=ifcfg-"$1"
SLAVE2=ifcfg-"$2"
BOND=ifcfg-"$3"
BOND_IPADDR="$4"
BOND_NETMASK="$5"
BOND_DIR=/etc/sysconfig/network-scripts
if [ -e $BOND_DIR/$BOND ]
then
 echo $BOND_DIR/$BOND is already exist
else
 #file backup
 Check_BakFile "${BOND_DIR}/ifcfg-*" "${BOND_DIR}/inspur_bak" "-M"

#get mac address
 SLAVE1_MAC=`grep 'HWADDR' ${BOND_DIR}/${SLAVE1}`
 SLAVE2_MAC=`grep 'HWADDR' ${BOND_DIR}/${SLAVE2}`

 # modify $BOND
 touch $BOND_DIR/$BOND
 echo "DEVICE=${BOND_DEV}" >> $BOND_DIR/$BOND
 echo "BOOTPROTO=none" >> $BOND_DIR/$BOND
 echo "ONBOOT=yes" >> $BOND_DIR/$BOND
 echo "TYPE=Ethernet" >> $BOND_DIR/$BOND
 echo "USERCTL=no" >> $BOND_DIR/$BOND
 echo "IPV6INIT=no" >> $BOND_DIR/$BOND
 echo "PEERDNS=yes" >> $BOND_DIR/$BOND
 echo "IPADDR=${BOND_IPADDR}" >> $BOND_DIR/$BOND
 echo "NETMASK=${BOND_NETMASK}" >> $BOND_DIR/$BOND
 if [[ ${get_primary_NIC} == none ]]; then
   echo "BONDING_OPTS=\"miimon=100 ${mode_bond}\"" >> $BOND_DIR/$BOND
 else
   echo "BONDING_OPTS=\"miimon=100 ${mode_bond} primary=$(echo ${SLAVE1} | cut -d'-' -f2)\"" >> $BOND_DIR/$BOND
 fi

 # modify $SLAVE1
 > $BOND_DIR/$SLAVE1
 echo "DEVICE=${SLAVE1_DEV}" >> $BOND_DIR/$SLAVE1
 echo 'BOOTPROTO=none' >> $BOND_DIR/$SLAVE1
 #cat $BOND_DIR/../ifcfg-bak/$SLAVE1 |grep HWADDR >> $BOND_DIR/$SLAVE1
 echo "#$SLAVE1_MAC" >> $BOND_DIR/$SLAVE1
 echo ONBOOT=yes >> $BOND_DIR/$SLAVE1
 echo TYPE=Ethernet >> $BOND_DIR/$SLAVE1
 echo USERCTL=no >> $BOND_DIR/$SLAVE1
 echo IPV6INIT=no >> $BOND_DIR/$SLAVE1
 echo PEERDNS=yes >> $BOND_DIR/$SLAVE1
 echo SLAVE=yes >> $BOND_DIR/$SLAVE1
 echo MASTER=$BOND_DEV >> $BOND_DIR/$SLAVE1

 # modify SLAVE2
 > $BOND_DIR/$SLAVE2
 echo "DEVICE=$SLAVE2_DEV" >> $BOND_DIR/$SLAVE2
 echo BOOTPROTO=none >> $BOND_DIR/$SLAVE2
 #cat $BOND_DIR/../ifcfg-bak/$SLAVE2 |grep HWADDR >> $BOND_DIR/$SLAVE2
 echo "#$SLAVE2_MAC" >> $BOND_DIR/$SLAVE2
 echo ONBOOT=yes >> $BOND_DIR/$SLAVE2
 echo TYPE=Ethernet >> $BOND_DIR/$SLAVE2
 echo USERCTL=no >> $BOND_DIR/$SLAVE2
 echo IPV6INIT=no >> $BOND_DIR/$SLAVE2
 echo PEERDNS=yes >> $BOND_DIR/$SLAVE2
 echo SLAVE=yes >> $BOND_DIR/$SLAVE2
 echo MASTER=$BOND_DEV >> $BOND_DIR/$SLAVE2

 [[ -e /etc/modprobe.conf.bak ]] && cp /etc/modprobe.conf /etc/modprobe.conf.bak.new || cp /etc/modprobe.conf /etc/modprobe.conf.bak
 echo "alias $BOND_DEV bonding" >> /etc/modprobe.conf

while true; do
 tput clear;tput cup 2
cat <<EOF
----------------------------------------------
------ Network Configuration Assistant ------
----------------------------------------------

Complete!
File backup directory: ${BOND_DIR}/inspur_bak

you can check the file and then restart the network service.

1 ) service network restart
2 ) exit
----------------------------------------------
EOF
  echo -e "Please make your choice:_\b\c"
  read answer
   case ${answer} in
    1 )
     service network restart
     exit 0
     ;;
    2 )
     exit 0
     ;;
   esac

 done
fi
}

main(){
 while true; do
  bonding_pre
 done
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Linux采用双网卡bond、起子接口的方式

    什么是bond 网卡bond是通过多张网卡绑定为一个逻辑网卡,实现本地网卡的冗余,带宽扩容和负载均衡,在生产场景中是一种常用的技术. 适用场景 服务器两张网卡需要做bond,并且bond后网卡需配置不同网段的地址,用于走不同流量,这个时候就可以采用起子接口的方式. 实验场景 设备 服务器:Server_A 核心交换机:Switch_A.Switch_B 交换机连接方式:堆叠 服务器网卡:enp176s0f0.enp176s0f1做bond IP段划分 业务段     VLAN 201:10.10

  • Linux双网卡绑定实现负载均衡详解

    Linux双网卡绑定实现负载均衡,供大家参考,具体内容如下 系统环境:CentOS release 6.9 (Final) Linux centos6 2.6.32-696.10.1.el6.x86_64 Ubuntu系统下使用ifenslave进行网卡配置,方法大同小异. 关闭NetworkManager # service NetworkManager stop # chkconfig NetworkManager off 创建bond0接口配置文件 # cd /etc/sysconfig/

  • Linux系统下双网卡配置实践总结

    前提: 1.不要在虚拟机上配置.不论是vm5还是vm6,本人试过多次,每次都是貌似成功,但用机器ping做断网试验时就会发现真相: 2.必须用同品牌同型号网卡.两块不一样的网卡,按照文档配置完毕后,即使从本机ping外部地址,也会发现网络似乎完全断了: 3.慎用service network restart.配置完毕,试验成功后,避免在双网卡机器上执行网络重启的操作,否则你会发现网络不通了,即使你手工再次执行ifenslave eth0 eth1也只能激活一块网卡. 下面说一下配置: 1.网卡配

  • linux 双网卡绑定网络设置方法

    设置linux主机的主机名和域名 /etc/hosts 例子 127.0.0.1 localhost.localdomain localhost 一般情况下hosts的内容关于主机名(hostname)的定义,每行为一个主机,每行由三部份组成,每个部份由空格隔开.其中#号开头的行做说明,不被系统解释. 第一部份:网络IP地址: 第二部份:主机名.域名,注意主机名和域名之间有个半角的点,比如 localhost.localdomain 第二部份:主机名(主机名别名) ,其实就是主机名: 当然每行也

  • Linux下双网卡Firewalld的配置流程(推荐)

    实验室拟态存储的项目需要通过LVS-NAT模式通过LVS服务器来区隔内外网的服务,所以安全防护的重心则落在了LVS服务器之上.笔者最终选择通过firewalld放行端口的方式来实现需求,由于firewall与传统Linux使用的iptable工具有不小的区别,接下来通过博客来记录一下firewalld的配置流程. 1.Firewall服务的简介: firewalld提供了一个 动态管理的防火墙,用以支持不同网络区域的规则,分配对一个网络及其相关链接和界面一定程度的信任.它具备对 IPv4 和 I

  • Linux双网卡绑定脚本的方法示例

    linux运维及配置工作中,常常会用到双网卡绑定,少数几台服务器的配置还好,如果是需要配置几十甚至上百台,难免会枯燥乏味,易于出错,我编写了这个双网卡绑定的辅助脚本,可傻瓜式地完成linux双网卡绑定工作,当然,该脚本主要还是用于小批量的系统配置,如需配置大量的服务器,可提取脚本中的bonding函数,稍作修改即可,你值得一试! 1.适用范围 该shell脚本可在以下linux系统创建多个绑定网卡,用于生产环境没问题的: Redhat 5.x CentOS 5.x Kylin 3.x KUX 2

  • linux 使用bond实现双网卡绑定单个IP的示例代码

    双网卡绑定单个IP 地址 为了提供网络的高可用性,我们可能需要将多块网卡绑定成一块虚拟网卡对外提供服务,这样即使其中的一块物理网卡出现故障,也不会导致连接中断. bond在Linux下叫bonding,IBM称为etherchanel,broadcom叫team,但是名字怎么变,效果都是将两块或更多的网卡当做一块网卡使用,在增加带宽的同时也可以提高冗余性. 实现双网卡绑定的方法有两种: bond和team 这里先记下bond的绑定方法 bond支持的模式 共支持bond[0-6]共七种模式,常用

  • Linux 系统双网卡绑定配置实现

    系统版本 [root@ ~]# cat /etc/redhat-release CentOS release 6.8 (Final) [root@ ~]# uname -r 2.6.32-642.6.1.el6.x86_64 网卡说明 eth0   192.168.1.8(服务器外网卡) eth1 eth2 两块服务器网卡(内网) 关闭防火墙 [root@ ~]# /etc/init.d/iptables stop [root@ ~]# chkconfig iptables off 关闭seli

  • Centos7/RHEL7双网卡绑定的方法

    1. 简要 双网卡绑定技术在centos7中使用了teaming技术,而在rhel6/centos7中使用的是bonding技术,在centos7中双网卡绑定既能使用teaming也可以使用bonding,这里推荐使用teaming技术,方便与查看和监控. 2. 原理 这里介绍两种最常见的双网卡绑定模式: (1) roundrobin - 轮询模式 所有链路处于负载均衡状态,这种模式的特点增加了带宽,同时支持容错能力. (2) activebackup - 主备模式 一个网卡处于活动状态,另一个

  • shell之定时周期性执行脚本的方法示例

    示例 1.编写测试脚本脚本 time .sh $ cat time.sh #! /bin/bash echo $(date +%s) >> /home/ocean/out.txt 保存完毕后记得给予权限 chmod 777 test.sh 2.添加定时任务 $ crontab -e no crontab for ocean - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/n

  • Centos 7.2中双网卡绑定及相关问题踩坑记录

    前言 最近工作中在做线上服务器,安装centos7.2 x64最小化安装,需要做链路聚合,双网卡绑定.在centos 6.x 和 centos 7上测试都OK,于是直接开搞. 说明下,以下环境是在虚拟机中实现的: 系统: centos7.2 x64 最小化安装. 为了方便演示,这里共有三张网卡: eno16777736 : 桥接网卡:10.0.0.11/24 剩下的两张网卡准备做绑定: eno33554984 eno50332208 [root@bogon ~]# nmcli con sh NA

  • linux 服务器自动备份脚本的方法(mysql、附件备份)

    一.创建backup.sh脚本文件 #!/bin/sh SOURCE_FOLDER=/data DB_FOLDER=/data/db BACKUP_FOLDER=/data/backup TM=`date +%Y%m%d%H%M%S` echo start to dump mysql database and backup files at $TM # delete old sql backup files cd $DB_FOLDER rm -rf $DB_FOLDER/*.sql #dump

  • Vue自动构建发布脚本的方法示例

    简介 使用cross-env, scp2两个插件完成 cross-env cross-env这是一款运行跨平台设置和使用环境变量的脚本. 为什么需要cross-env? NODE_ENV=production 像这样设置环境变量时,大多数Windows命令提示符都会阻塞 .(Windows上的Bash是例外,它使用本机Bash.)同样,Windows和POSIX命令使用环境变量的方式也有所不同.对于POSIX,您可以使用: $ENV_VAR 和在Windows上可以使用 %ENV_VAR% .

随机推荐