欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

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

程序员文章站 2022-04-26 12:43:28
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
}

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