• 售前

  • 售后

热门帖子
入门百科

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

[复制链接]
刘得福姊妹 显示全部楼层 发表于 2021-10-25 20:25:38 |阅读模式 打印 上一主题 下一主题
linux运维及配置工作中,经常会用到双网卡绑定,少数几台服务器的配置还好,如果是需要配置几十乃至上百台,不免会枯燥乏味,易于出错,我编写了这个双网卡绑定的辅助脚本,可傻瓜式地完成linux双网卡绑定工作,固然,该脚本紧张还是用于小批量的系统配置,如需配置大量的服务器,可提取脚本中的bonding函数,稍作修改即可,你值得一试!
1.实用范围

该shell脚本可在以下linux系统创建多个绑定网卡,用于生产环境没问题的:
      
  • Redhat 5.x  
  • CentOS 5.x  
  • Kylin 3.x  
  • KUX 2.x
2.脚本特点
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. The information you have entered:
  5. First NIC   :eth0
  6. Second NIC  :eth1
  7. bond name   :bond1
  8. IP address  :192.168.56.11
  9. netmask    :255.255.255.0
  10. bonding_mode :mode=1
  11. primary NIC  :none
  12. ----------------------------------------------
  13. Pls make sure its OK[y/n]:y
复制代码
如上,脚本在利用过程中,会依次提示输入需要绑定的子网卡名称、绑定网卡名称、IP地点和子网掩码、绑定模式、是否指定主网卡,并主动对系统中可用网卡进行辨认并列出,供用户选择,同时会判定输入的各项信息的精确性,保障双网卡绑定顺利进行。
3.利用分析

起首将脚本上传至linux系统恣意目次下,利用root用户实行:
  1. [root@node1 tmp]# sh bonding_v2.0.sh
  2. ----------------------------------------------
  3. ------ Network Configuration Assistant ------
  4. ----------------------------------------------
  5. 4 network cards available:
  6. eth0 eth1 eth2 eth3
  7. ----------------------------------------------
  8. Please enter the First NIC:_
复制代码
起首脚本会列出系统中有多少可用网卡,并提示输入需要绑定的第一块子网卡的名称,这里输入eth2。
无需担心输入错误,输入重复等问题,脚本会主动判定你的输入信息是否精确。
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. 4 network cards available:
  5. eth0 eth1 eth2 eth3
  6. The information you have entered:
  7. First NIC   : eth2
  8. ----------------------------------------------
  9. Please select the second NIC:_
复制代码
接着输入需要绑定的第二块网卡的名称eth3,如果输入错误会出现相应的提示,好比错输入为eth8,会提示需要重新输入:
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. 4 network cards available:
  5. eth0 eth1 eth2 eth3
  6. The information you have entered:
  7. First NIC   : eth2
  8. ----------------------------------------------
  9. eth8 is not available,Please enter another one.
  10. Please select the second NIC:_
复制代码
精确输入需要绑定的第二块网卡eth3后,如果linux系统中之前不存在绑定网卡,则默认第一个绑定网卡的名称为bond0,如果系统中存在绑定网卡,脚本会提示输入bondN(N为数字):
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. The information you have entered:
  5. First NIC   : eth2
  6. Second NIC  : eth3
  7. bond name   : bond0
  8. ----------------------------------------------
  9. The default first bond name is -- bond0.
  10. Please enter an IP address:_
复制代码
按照提示输入需要绑定的IP地点,好比10.1.1.1
脚本会主动判定输入的IP地点格式和范围是否精确,无需担心输入错误
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. The information you have entered:
  5. First NIC   : eth2
  6. Second NIC  : eth3
  7. bond name   : bond0
  8. IP address  : 10.1.1.1
  9. ----------------------------------------------
  10. Pls enter the NETMASK[255.255.255.0]:_
复制代码
输入IP地点后,会提示输入子网掩码,默以为255.255.255.0,直接回车即可,也可以输入自界说的子网掩码。
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. The information you have entered:
  5. First NIC   : eth2
  6. Second NIC  : eth3
  7. bond name   : bond0
  8. IP address  : 10.1.1.1
  9. netmask    : 255.255.255.0
  10. ----------------------------------------------
  11. Pls enter the bonding_mode[default:mode=1,active-backup]:_
复制代码
这一步是输入网卡绑定级别,默认利用主备模式:mode=1,直接回车即可,也可以输入自界说的绑定级别(如输入 mode=6),格式为:
  1. mode={0,1,2,3,4,5,6}
  2. ----------------------------------------------
  3. ------ Network Configuration Assistant ------
  4. ----------------------------------------------
  5. The information you have entered:
  6. First NIC   :eth2
  7. Second NIC  :eth3
  8. bond name   :bond0
  9. IP address  :10.1.1.1
  10. netmask    :255.255.255.0
  11. bonding_mode :mode=1
  12. ----------------------------------------------
  13. 1-eth2
  14. 2-eth3
  15. 3-none
  16. choose whether to set up the primary NIC or not[default:3-none]:_
复制代码
此时脚本提示是否设置primary网卡,primary网卡的作用是在子网卡均正常时,primary网卡优先处于活动状态,这里默以为不设置primary网卡,也可以输入数字1或2选择一个网卡为primary网卡。
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. The information you have entered:
  5. First NIC   :eth2
  6. Second NIC  :eth3
  7. bond name   :bond0
  8. IP address  :10.1.1.1
  9. netmask    :255.255.255.0
  10. bonding_mode :mode=1
  11. primary NIC  :none
  12. ----------------------------------------------
  13. Pls make sure its OK[y/n]:_
复制代码
至此,所有信息输入完毕,脚本会让您判定输入的所有信息是否精确,精确则输入y,进行下一步操纵,否则输入n,重新输入以上信息。
  1. ----------------------------------------------
  2. ------ Network Configuration Assistant ------
  3. ----------------------------------------------
  4. Complete!
  5. File backup directory: /etc/sysconfig/network-scripts/inspur_bak
  6. you can check the file and then restart the network service.
  7. 1 ) service network restart
  8. 2 ) exit
  9. ----------------------------------------------
  10. Please make your choice:_
复制代码
看到这里时,所有绑定网卡的配置文件已经修改完毕了,输入1重启网络服务;输入2直接退出脚本。
发起选择重启网络服务,如果直接退出脚本而不重启网络服务,修改的配置文件是不访问效的,而且在利用该脚本创建下一个绑定网卡时表现信息会出现异常(因为脚本通过ifconfig下令获取当前网卡信息)。
  1. you can check the file and then restart the network service.
  2. 1 ) service network restart
  3. 2 ) exit
  4.   ----------------------------------------------
  5. Please make your choice:1
  6. 正在关闭接口 eth0:                    [确定]
  7. 正在关闭接口 eth1:                    [确定]
  8. 正在关闭接口 eth2:                    [确定]
  9. 正在关闭接口 eth3:                    [确定]
  10. 关闭环回接口:                       [确定]
  11. 弹出环回接口:                       [确定]
  12. 弹出界面 bond0: RTNETLINK answers: File exists
  13. 在 bond0 添加地址 10.1.1.1 时出错。
  14.                               [确定]
  15. 弹出界面 eth0:                      [确定]
  16. 弹出界面 eth1:                      [确定]
复制代码
第一次重启网络服务时大概会出现以上雷同报错,无需担心,其实配置已经完成,再次重启网络服务即规复正常。
在绑定过程中修改的配置文件如下:
  1. [root@node1 tmp]# cd /etc/sysconfig/network-scripts/
  2. [root@node1 network-scripts]# cat ifcfg-bond0
  3. DEVICE=bond0
  4. BOOTPROTO=none
  5. ONBOOT=yes
  6. TYPE=Ethernet
  7. USERCTL=no
  8. IPV6INIT=no
  9. PEERDNS=yes
  10. IPADDR=10.1.1.1
  11. NETMASK=255.255.255.0
  12. BONDING_OPTS="miimon=100 mode=1"
  13. [root@node1 network-scripts]# cat ifcfg-eth2
  14. DEVICE=eth2
  15. BOOTPROTO=none
  16. #HWADDR=08:00:27:41:b4:16
  17. ONBOOT=yes
  18. TYPE=Ethernet
  19. USERCTL=no
  20. IPV6INIT=no
  21. PEERDNS=yes
  22. SLAVE=yes
  23. MASTER=bond0
  24. [root@node1 network-scripts]# cat ifcfg-eth3
  25. DEVICE=eth3
  26. BOOTPROTO=none
  27. #HWADDR=08:00:27:6c:3d:0c
  28. ONBOOT=yes
  29. TYPE=Ethernet
  30. USERCTL=no
  31. IPV6INIT=no
  32. PEERDNS=yes
  33. SLAVE=yes
  34. MASTER=bond0
  35. [root@node1 network-scripts]# cat /etc/modprobe.conf
  36. alias scsi_hostadapter ata_piix
  37. alias scsi_hostadapter1 ahci
  38. alias net-pf-10 off
  39. alias ipv6 off
  40. options ipv6 disable=1
  41. alias snd-card-0 snd-intel8x0
  42. options snd-card-0 index=0
  43. options snd-intel8x0 index=0
  44. remove snd-intel8x0 { /usr/sbin/alsactl store 0 >/dev/null 2>&1 || : ; }; /sbin/modprobe -r --ignore-remove snd-intel8x0
  45. alias eth0 e1000
  46. alias eth1 e1000
  47. alias eth2 e1000
  48. alias eth3 e1000
  49. alias bond0 bonding
复制代码
网络配置信息如下:
  1. [root@node1 tmp]# ifconfig
  2. bond0   Link encap:Ethernet HWaddr 08:00:27:41:B4:16
  3.      inet addr:10.1.1.1 Bcast:10.1.1.255 Mask:255.255.255.0
  4.      UP BROADCAST RUNNING MASTER MULTICAST MTU:1500 Metric:1
  5.      RX packets:1197 errors:0 dropped:0 overruns:0 frame:0
  6.      TX packets:125 errors:0 dropped:0 overruns:0 carrier:0
  7.      collisions:0 txqueuelen:0
  8.      RX bytes:106338 (103.8 KiB) TX bytes:16662 (16.2 KiB)
  9. eth0   Link encap:Ethernet HWaddr 08:00:27:35:B4:81
  10.      inet addr:192.168.56.11 Bcast:192.168.56.255 Mask:255.255.255.0
  11.      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  12.      RX packets:1487 errors:0 dropped:0 overruns:0 frame:0
  13.      TX packets:1190 errors:0 dropped:0 overruns:0 carrier:0
  14.      collisions:0 txqueuelen:1000
  15.      RX bytes:146576 (143.1 KiB) TX bytes:151787 (148.2 KiB)
  16. eth1   Link encap:Ethernet HWaddr 08:00:27:72:08:46
  17.      inet addr:10.1.1.11 Bcast:10.1.1.255 Mask:255.255.255.0
  18.      UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
  19.      RX packets:193 errors:0 dropped:0 overruns:0 frame:0
  20.      TX packets:455 errors:0 dropped:0 overruns:0 carrier:0
  21.      collisions:0 txqueuelen:1000
  22.      RX bytes:31434 (30.6 KiB) TX bytes:22657 (22.1 KiB)
  23. eth2   Link encap:Ethernet HWaddr 08:00:27:41:B4:16
  24.      UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
  25.      RX packets:591 errors:0 dropped:0 overruns:0 frame:0
  26.      TX packets:93 errors:0 dropped:0 overruns:0 carrier:0
  27.      collisions:0 txqueuelen:1000
  28.      RX bytes:55217 (53.9 KiB) TX bytes:10511 (10.2 KiB)
  29. eth3   Link encap:Ethernet HWaddr 08:00:27:41:B4:16
  30.      UP BROADCAST RUNNING SLAVE MULTICAST MTU:1500 Metric:1
  31.      RX packets:606 errors:0 dropped:0 overruns:0 frame:0
  32.      TX packets:32 errors:0 dropped:0 overruns:0 carrier:0
  33.      collisions:0 txqueuelen:1000
  34.      RX bytes:51121 (49.9 KiB) TX bytes:6151 (6.0 KiB)
  35. lo    Link encap:Local Loopback
  36.      inet addr:127.0.0.1 Mask:255.0.0.0
  37.      UP LOOPBACK RUNNING MTU:16436 Metric:1
  38.      RX packets:3297 errors:0 dropped:0 overruns:0 frame:0
  39.      TX packets:3297 errors:0 dropped:0 overruns:0 carrier:0
  40.      collisions:0 txqueuelen:0
  41.      RX bytes:4725726 (4.5 MiB) TX bytes:4725726 (4.5 MiB)
复制代码
双网卡绑定信息:
  1. [root@node1 tmp]# cat /proc/net/bonding/bond0
  2. Ethernet Channel Bonding Driver: v3.4.0-2 (October 7, 2008)
  3. Bonding Mode: fault-tolerance (active-backup)
  4. Primary Slave: None
  5. Currently Active Slave: eth2
  6. MII Status: up
  7. MII Polling Interval (ms): 100
  8. Up Delay (ms): 0
  9. Down Delay (ms): 0
  10. Slave Interface: eth2
  11. MII Status: up
  12. Speed: 1000 Mbps
  13. Duplex: full
  14. Link Failure Count: 0
  15. Permanent HW addr: 08:00:27:41:b4:16
  16. Slave Interface: eth3
  17. MII Status: up
  18. Speed: 1000 Mbps
  19. Duplex: full
  20. Link Failure Count: 0
  21. Permanent HW addr: 08:00:27:6c:3d:0c
复制代码
4.脚本代码
  1. #!/bin/bash
  2. # Date:2018-11-26 14:28:45
  3. # Author:ZhangJian
  4. # Mail:1037509307@qq.com
  5. # Func:Configure Network Bonding
  6. # Version:2.0
  7. #V1.6版本更新:不再将绑定的模式mode=x写入modprobe.conf文件,将其写入ifcfg-bond中,因为在modprobe.conf文件中时,修改了mode模式后,重启网络服务可能不生效。
  8. #V2.0版本更新:将primary网卡设置为可选择项。
  9. trap "tput clear;tput cup 3;echo 'Any Questions: Send a message to QQ 1037509307.';tput cup 6;exit" 2 3
  10. if [[ -n $1 ]]; then
  11. cat <<EOF
  12. Network Configuration Assistant
  13.           --- Configure Network Bonding v1.5
  14. Usage:
  15.     sh $0
  16.     chmod +x $0 && ./$0
  17. EOF
  18. exit 0
  19. fi
  20. if [[ $UID -ne 0 ]]; then
  21. tput clear
  22. tput cup 6 20
  23. echo -e "You must use the user: \033[31mROOT\033[0m"
  24. tput cup 10
  25. exit
  26. fi
  27. ERROR(){
  28. tput cup $1 $2;tput ed
  29. echo 'Input error,Try again pls.'
  30. echo -e 'Press ENTER to continue..._\b\c'
  31. read inputA
  32. }
  33. Check_BakFile(){
  34. #文件备份函数
  35. #使用方法:
  36. #Check_BakFile 要备份的文件名 备份目录 -x(按什么时间格式备份)
  37. case ${3} in
  38.   -d ) #按天备份
  39.     Bak_Date=`date '+%Y-%m-%d'`
  40.     ;;
  41.   -H ) #按小时备份
  42.     Bak_Date=`date '+%Y-%m-%d_%H'`
  43.     ;;
  44.   -M ) #按分钟备份
  45.     Bak_Date=`date '+%Y-%m-%d_%H:%M'`
  46.     ;;
  47.   -m ) #按月备份
  48.     Bak_Date=`date '+%Y-%m'`
  49.     ;;
  50.   -Y ) #按年备份
  51.     Bak_Date=`date '+%Y'`
  52.     ;;
  53.   * ) #默认按分钟备份
  54.     Bak_Date=`date '+%Y-%m-%d_%H:%M'`
  55.     ;;
  56. esac
  57. #Bak_Date=`date '+%Y-%m-%d-%H:%M'`
  58. [[ -d ${2}/${Bak_Date} ]] || mkdir -p ${2}/${Bak_Date}
  59. cp -ra ${1} ${2}/${Bak_Date}
  60. }
  61. bonding_pre(){
  62. #双网卡绑定前导函数,判定网卡、IP等合法性并引导用户正确输入参数。
  63. #全部真实网卡
  64. NIC_NAME_all=`ifconfig -a | awk '/\<Ethernet\>/ {print $1}' | grep -wEv '^bond[0-9]+'`
  65. #已经存在的绑定网卡
  66. NIC_NAME_bond=`ifconfig -a | awk '/^\<bond[0-9]+\>/ {print $1}'`
  67. #打印出已经是SLAVE的网卡
  68. NIC_NAME_slave=`ifconfig -a | sed -n '/SLAVE/{g;1!p;};h' | awk '{print $1}'`
  69. NIC_NAME_slave=${NIC_NAME_slave:=NULL}
  70. #打印出可用网卡
  71. NIC_NAME_free=`echo "$NIC_NAME_all" | grep -Fwv "$NIC_NAME_slave"`
  72. #可用网卡数量
  73. NIC_NAME_free_nu=`echo "$NIC_NAME_free" | wc -w`
  74. declare -a NIC_LIST
  75. NIC_LIST=($NIC_NAME_free)
  76. info_print(){
  77. tput clear;tput cup 2
  78. cat <<EOF
  79. ----------------------------------------------
  80. ------ Network Configuration Assistant ------
  81. ----------------------------------------------
  82. EOF
  83. tput cup 7
  84. if [[ -n ${1} ]]; then
  85. echo -e "Already existing Channel Bonding Interface of the system:\n\033[31m${1}\033[0m\n"
  86. fi
  87. }
  88. if [[ "$NIC_NAME_free_nu" -gt 1 ]]; then
  89. info_print
  90. echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:"
  91. echo -e "\033[31m${NIC_LIST[@]}\033[0m"
  92. echo -e "----------------------------------------------\n"
  93. #输入第一块网卡的名称
  94. while true; do
  95.   echo -e "\nPlease enter the First NIC:_\b\c"
  96.   read NIC1
  97.   echo ${NIC_LIST[@]} | grep -Fw "$NIC1" &> /dev/null
  98.   if [[ $? -eq 0 ]]; then
  99.    info_print
  100.    echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:\n\033[31m${NIC_LIST[@]}\033[0m"
  101.    echo -e "\nThe information you have entered:\nFirst NIC   : $NIC1"
  102.    echo -e "----------------------------------------------\n"
  103.    break
  104.   else
  105.    info_print
  106.    echo -e "\033[031m$NIC_NAME_free_nu\033[0m network cards available:\n\033[31m${NIC_LIST[@]}\033[0m"
  107.    echo -e "----------------------------------------------\n"
  108.    echo -e "\033[31m${NIC1}\033[0m is not available,Please enter another one."
  109.   fi
  110. done
  111. #输入第二块网卡的名称
  112. while true; do
  113.   echo -e "\nPlease select the second NIC:_\b\c"
  114.   read NIC2
  115.   if [[ ${NIC1} != ${NIC2} ]]; then
  116.    echo ${NIC_LIST[@]} | grep -Fw "${NIC2}" &> /dev/null
  117.    if [[ $? -eq 0 ]]; then
  118.     info_print "${NIC_NAME_bond}"
  119.     echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}"
  120.     echo -e "----------------------------------------------\n"
  121.     break
  122.    else
  123.     info_print
  124.     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}"
  125.     echo -e "----------------------------------------------\n"
  126.     echo -e "\033[31m${NIC2}\033[0m is not available,Please enter another one."
  127.    fi
  128.   else
  129.    info_print
  130.    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}"
  131.    echo -e "----------------------------------------------\n"
  132.    echo -e "\033[31m${NIC2}\033[0m is the first NIC,Please enter another one."
  133.   fi
  134. done
  135. #输入绑定网卡名称并进行合理性检查
  136. if [[ -z ${NIC_NAME_bond} ]]; then
  137.   NAME_bond=bond0
  138.   info_print
  139.   echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}"
  140.   echo -e "----------------------------------------------\n"
  141.   echo "The default first bond name is -- bond0."
  142. else
  143.   while true; do
  144.    echo -e "\nPlease enter a bond name[bonN]:_\b\c"
  145.    read NAME_bond
  146.    #检查输入格式是否为bond+数字的格式。
  147.    echo "${NAME_bond}" | grep -wE '^bond[[:digit:]]+$' &> /dev/null
  148.    if [[ $? -eq 0 ]]; then
  149.     echo "${NIC_NAME_bond}" | grep -Fw "${NAME_bond}" &> /dev/null
  150.     if [[ $? -ne 0 ]]; then
  151.      info_print "${NIC_NAME_bond}"
  152.      echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}"
  153.      echo -e "----------------------------------------------\n"
  154.      break
  155.     else
  156.      info_print "${NIC_NAME_bond}"
  157.      echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}"
  158.      echo -e "----------------------------------------------\n"
  159.      echo "You can not use an existing name: ${NAME_bond}"
  160.      echo -e "\033[31m${NAME_bond}\033[0m is not available,Please enter another one like -- bondN."
  161.     fi
  162.    else
  163.     info_print "${NIC_NAME_bond}"
  164.     echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}"
  165.     echo -e "----------------------------------------------\n"
  166.     echo -e "\033[31m${NAME_bond}\033[0m is not available,Please enter another one like -- bondN."
  167.    fi
  168.   done
  169. fi
  170. #设置IP地址
  171. while true; do
  172.   echo -e "\nPlease enter an IP address:_\b\c"
  173.   read IP_bond
  174.   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
  175.   if [[ $? -eq 0 ]]; then
  176.    info_print
  177.    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}"
  178.    echo -e "----------------------------------------------\n"
  179.    break
  180.   else
  181.    info_print
  182.    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}"
  183.    echo -e "----------------------------------------------\n"
  184.    echo -e "IP address: \033[31m${IP_bond}\033[0m format errors, please re-enter."
  185.   fi
  186. done
  187. #设置netmask
  188. while true; do
  189.   echo -e "\nPls enter the NETMASK[255.255.255.0]:_\b\c"
  190.   read NETMASK_bond
  191.   NETMASK_bond=${NETMASK_bond:="255.255.255.0"}  #当变量为NETMASK_bond为空时,给其赋默认值255.255.255.0
  192.   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
  193.   if [[ $? -eq 0 ]]; then
  194.    info_print
  195.    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}\nnetmask    : ${NETMASK_bond}"
  196.    echo -e "----------------------------------------------\n"
  197.    break
  198.   else
  199.    info_print
  200.    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}"
  201.    echo -e "----------------------------------------------\n"
  202.    echo -e "Input error.\nPlease enter the correct NETMASK or press ENTER to use 255.255.255.0.\n"
  203.   fi
  204. done
  205. #:<<!zhushi! #批量注释,如果开启批量注释,默认mode=1
  206. while true; do
  207.   echo -e "\nPls enter the bonding_mode[default:mode=1,active-backup]:_\b\c"
  208.   read mode_bond
  209.   mode_bond=${mode_bond:="mode=1"}  #当变量为mode_bond为空时,给其赋默认值1
  210.   if [[ ! ${mode_bond} =~ "mode=[0-6]" ]]; then
  211.    info_print
  212.    echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}\netmask    : ${NETMASK_bond}"
  213.    echo -e "----------------------------------------------\n"
  214.    echo "Invalid answer: ${mode_bond}"
  215.    echo "Eg: mode={0,1,2,3,4,5,6}"
  216.   else
  217.    info_print
  218.    echo -e "The information you have entered:\n"
  219.    echo -e "First NIC   :${NIC1}\nSecond NIC  :${NIC2}\nbond name   :${NAME_bond}\nIP address  :${IP_bond}\nnetmask    :${NETMASK_bond}\nbonding_mode :${mode_bond}"
  220.    echo -e "----------------------------------------------\n"
  221.    break
  222.   fi
  223. done
  224. #!zhushi!
  225. #选择是否设置primary网卡
  226. while true; do
  227.   echo "1-${NIC1}"
  228.   echo "2-${NIC2}"
  229.   echo "3-none"
  230.   echo -e "choose whether to set up the primary NIC or not[default:3-none]:_\b\c"
  231.   read get_primary_NIC
  232.   get_primary_NIC=${get_primary_NIC:="none"}
  233.     case ${get_primary_NIC} in
  234.     1 )
  235.      get_primary_NIC=${NIC1}
  236.      #echo "--$get_primary_NIC"
  237.      break
  238.      ;;
  239.     2 )
  240.      get_primary_NIC=${NIC2}
  241.       #echo "--$get_primary_NIC"
  242.      break
  243.      ;;
  244.     3 )
  245.      get_primary_NIC=none
  246.       #echo "--$get_primary_NIC"
  247.      break
  248.      ;;
  249.     none )
  250.      break
  251.      ;;
  252.     * )
  253.       #echo "--$get_primary_NIC"
  254.      info_print
  255.      echo -e "The information you have entered:\nFirst NIC   : ${NIC1}\nSecond NIC  : ${NIC2}\nbond name   : ${NAME_bond}\nIP address  : ${IP_bond}\nnetmask    : ${NETMASK_bond}"
  256.      echo -e "----------------------------------------------\n"
  257.      echo "Invalid answer: ${get_primary_NIC}"
  258.      echo "Pls input a number in {1,2,3} or press ENTER to set up primary NIC none."
  259.      continue
  260.      ;;
  261.    esac
  262.    info_print
  263.    echo -e "The information you have entered:\n"
  264.    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}"
  265.    echo -e "----------------------------------------------\n"
  266.    break
  267. done
  268. #最终输入信息确认
  269. while true; do
  270.   info_print
  271.   echo -e "The information you have entered:\n"
  272.   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"
  273.   echo -e "----------------------------------------------\n"
  274.   echo -e "Pls make sure its OK[y/n]:_\b\c"
  275.   read input
  276.   case ${input} in
  277.    [Yy]|[Yy][Ee][Ss] )
  278.     bonding "$NIC1" "$NIC2" "$NAME_bond" "$IP_bond" "$NETMASK_bond"
  279.     break
  280.     ;;
  281.    [Nn]|[Nn][Oo] )
  282.     bonding_pre
  283.    ;;
  284.    * )
  285.     tput cup 7;tput ed
  286.     ERROR 7 10
  287.    ;;
  288.   esac
  289. done
  290. else
  291.   info_print
  292.   echo -e "You have \033[031m${NIC_NAME_free_nu}\033[0m network cards available:\n"
  293.   #将重点显示的“网卡名”标示为红色
  294.   echo -e "\033[31m${NIC_LIST[@]}\033[0m\n"
  295.   echo -e "There are \033[31mnot enough\033[0m network cards to make bonding"
  296.   echo -e "Pls check it......\n"
  297.   echo -e 'Press ENTER to exit..._\b\c'
  298.   read answer
  299.   exit 1
  300. fi
  301. }
  302. bonding(){
  303. if [[ $# -lt 5 ]]
  304. then
  305. echo 'Bonding failed! Please provide enough information!'
  306. echo -e "\nUsage:\n    sh bonding.sh <NIC1_name> <NIC2_name> <bond_name> <IP> <NETMASK>\n\n"
  307. exit 1
  308. fi
  309. #get device name and ip information
  310. SLAVE1_DEV="$1"    #SLAVE1_DEV=ethx
  311. SLAVE2_DEV="$2"    #SLAVE2_DEV=ethx
  312. BOND_DEV="$3"     #BOND_DEV=bondx
  313. SLAVE1=ifcfg-"$1"
  314. SLAVE2=ifcfg-"$2"
  315. BOND=ifcfg-"$3"
  316. BOND_IPADDR="$4"
  317. BOND_NETMASK="$5"
  318. BOND_DIR=/etc/sysconfig/network-scripts
  319. if [ -e $BOND_DIR/$BOND ]
  320. then
  321. echo $BOND_DIR/$BOND is already exist
  322. else
  323. #file backup
  324. Check_BakFile "${BOND_DIR}/ifcfg-*" "${BOND_DIR}/inspur_bak" "-M"
  325. #get mac address
  326. SLAVE1_MAC=`grep 'HWADDR' ${BOND_DIR}/${SLAVE1}`
  327. SLAVE2_MAC=`grep 'HWADDR' ${BOND_DIR}/${SLAVE2}`
  328. # modify $BOND
  329. touch $BOND_DIR/$BOND
  330. echo "DEVICE=${BOND_DEV}" >> $BOND_DIR/$BOND
  331. echo "BOOTPROTO=none" >> $BOND_DIR/$BOND
  332. echo "ONBOOT=yes" >> $BOND_DIR/$BOND
  333. echo "TYPE=Ethernet" >> $BOND_DIR/$BOND
  334. echo "USERCTL=no" >> $BOND_DIR/$BOND
  335. echo "IPV6INIT=no" >> $BOND_DIR/$BOND
  336. echo "PEERDNS=yes" >> $BOND_DIR/$BOND
  337. echo "IPADDR=${BOND_IPADDR}" >> $BOND_DIR/$BOND
  338. echo "NETMASK=${BOND_NETMASK}" >> $BOND_DIR/$BOND
  339. if [[ ${get_primary_NIC} == none ]]; then
  340.    echo "BONDING_OPTS="miimon=100 ${mode_bond}"" >> $BOND_DIR/$BOND
  341. else
  342.    echo "BONDING_OPTS="miimon=100 ${mode_bond} primary=$(echo ${SLAVE1} | cut -d'-' -f2)"" >> $BOND_DIR/$BOND
  343. fi
  344. # modify $SLAVE1
  345. > $BOND_DIR/$SLAVE1
  346. echo "DEVICE=${SLAVE1_DEV}" >> $BOND_DIR/$SLAVE1
  347. echo 'BOOTPROTO=none' >> $BOND_DIR/$SLAVE1
  348. #cat $BOND_DIR/../ifcfg-bak/$SLAVE1 |grep HWADDR >> $BOND_DIR/$SLAVE1
  349. echo "#$SLAVE1_MAC" >> $BOND_DIR/$SLAVE1
  350. echo ONBOOT=yes >> $BOND_DIR/$SLAVE1
  351. echo TYPE=Ethernet >> $BOND_DIR/$SLAVE1
  352. echo USERCTL=no >> $BOND_DIR/$SLAVE1
  353. echo IPV6INIT=no >> $BOND_DIR/$SLAVE1
  354. echo PEERDNS=yes >> $BOND_DIR/$SLAVE1
  355. echo SLAVE=yes >> $BOND_DIR/$SLAVE1
  356. echo MASTER=$BOND_DEV >> $BOND_DIR/$SLAVE1
  357. # modify SLAVE2
  358. > $BOND_DIR/$SLAVE2
  359. echo "DEVICE=$SLAVE2_DEV" >> $BOND_DIR/$SLAVE2
  360. echo BOOTPROTO=none >> $BOND_DIR/$SLAVE2
  361. #cat $BOND_DIR/../ifcfg-bak/$SLAVE2 |grep HWADDR >> $BOND_DIR/$SLAVE2
  362. echo "#$SLAVE2_MAC" >> $BOND_DIR/$SLAVE2
  363. echo ONBOOT=yes >> $BOND_DIR/$SLAVE2
  364. echo TYPE=Ethernet >> $BOND_DIR/$SLAVE2
  365. echo USERCTL=no >> $BOND_DIR/$SLAVE2
  366. echo IPV6INIT=no >> $BOND_DIR/$SLAVE2
  367. echo PEERDNS=yes >> $BOND_DIR/$SLAVE2
  368. echo SLAVE=yes >> $BOND_DIR/$SLAVE2
  369. echo MASTER=$BOND_DEV >> $BOND_DIR/$SLAVE2
  370. [[ -e /etc/modprobe.conf.bak ]] && cp /etc/modprobe.conf /etc/modprobe.conf.bak.new || cp /etc/modprobe.conf /etc/modprobe.conf.bak
  371. echo "alias $BOND_DEV bonding" >> /etc/modprobe.conf
  372. while true; do
  373. tput clear;tput cup 2
  374. cat <<EOF
  375. ----------------------------------------------
  376. ------ Network Configuration Assistant ------
  377. ----------------------------------------------
  378. Complete!
  379. File backup directory: ${BOND_DIR}/inspur_bak
  380. you can check the file and then restart the network service.
  381. 1 ) service network restart
  382. 2 ) exit
  383. ----------------------------------------------
  384. EOF
  385.   echo -e "Please make your choice:_\b\c"
  386.   read answer
  387.    case ${answer} in
  388.     1 )
  389.      service network restart
  390.      exit 0
  391.      ;;
  392.     2 )
  393.      exit 0
  394.      ;;
  395.    esac
  396. done
  397. fi
  398. }
  399. main(){
  400. while true; do
  401.   bonding_pre
  402. done
  403. }
复制代码
以上就是本文的全部内容,渴望对大家的学习有所帮助,也渴望大家多多支持脚本之家。

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作