配置双网卡绑定是运维的基本工作之一,第一次配置会觉得很稀奇,第十次配置会觉得不过尔尔,第三十次配置可以两分钟搞定,但第一百次配置的时候会有一种吐血的感觉,那怎么办呢?果断懒人原理,写脚本,自动化配置。

   
   
   
   
  1. #!/bin/bash 
  2. #double network cards bond 
  3. #write by xiaojing.zhao 
  4. #2012.11.19 
  5.  
  6. cd /etc/sysconfig/network-scripts 
  7. cp ifcfg-eth0 /tmp/ifcfg-eth0.bak 
  8. cp ifcfg-eth1 /tmp/ifcfg-eth1.bak 
  9. #cp ifcfg-eth0 ifcfg-bond0 
  10.  
  11. #vim eth0 
  12. cat ifcfg-eth0 | grep -i hwaddr > ifcfg-eth0.txt 
  13. #在HWADDR匹配行前插入SLAVE=yes,以此类推,此处也可以用echo输入来完成 
  14. sed -i '/HWADDR/ i SLAVE=yes' ifcfg-eth0.txt 
  15. sed -i '/SLAVE/ i MASTER=bond0' ifcfg-eth0.txt 
  16. sed -i '/MASTER/ i BOOTPROTO=none' ifcfg-eth0.txt 
  17. sed -i '/BOOTPROTO/ i DEVICE=eth0' ifcfg-eth0.txt 
  18. sed -i '/HWADDR/ a IPV6INIT=no' ifcfg-eth0.txt 
  19. sed -i '/IPV6INIT/ a ONBOOT=yes' ifcfg-eth0.txt 
  20. sed -i '/ONBOOT/ a TYPE=Ethernet' ifcfg-eth0.txt 
  21. sed -i '/TYPE/ a PEERDNS=yes' ifcfg-eth0.txt 
  22. sed -i '/PEERDNS/ a USERCTL=no' ifcfg-eth0.txt 
  23. cat ifcfg-eth0.txt > ifcfg-eth0 
  24.  
  25. #vim eth1 
  26. #在匹配DEVICE行,将(y指所有)0替换成1,即eth0替换为eth1 
  27. sed -i '/DEVICE/y/0/1/' ifcfg-eth0.txt 
  28. #变量linehwaddr为取出eth1的MAC地址``为通配符 
  29. linehwaddr=`cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep -i hwaddr` 
  30. #将eth0的MAC的地址替换为eth1的MAC地址 
  31. sed -i '/HWADDR/c\'"$linehwaddr"'' ifcfg-eth0.txt 
  32. cat ifcfg-eth0.txt > ifcfg-eth1 
  33.  
  34. #vim bond0 
  35. #脚本通用,只需改下列后五个网段,网址,网关,广播地址,子网掩码即可,按实际情况处理
  36. echo "DEVICE=bond0" > ifcfg-bond0 
  37. echo "BOOTPROTO=none" >> ifcfg-bond0 
  38. echo "IPV6INIT=no" >> ifcfg-bond0 
  39. echo "ONBOOT=yes" >> ifcfg-bond0 
  40. echo "TYPE=Ethernet" >> ifcfg-bond0 
  41. echo "PEERDNS=yes" >> ifcfg-bond0 
  42. echo "USERCTL=no" >> ifcfg-bond0 
  43. echo "NETMASK=255.255.255.0" >> ifcfg-bond0 
  44. echo "IPADDR=10.1.1.250" >> ifcfg-bond0 
  45. echo "GATEWAY=10.1.1.1" >> ifcfg-bond0 
  46. echo "BROADCAST=10.1.1.255" >> ifcfg-bond0 
  47. echo "NETWORK=10.1.1.0" >> ifcfg-bond0 
  48.  
  49. #vim /etc/modprobe.conf 
  50. #追加 
  51. echo "alias bond0 bonding" >> /etc/modprobe.conf 
  52. echo "options bond0 miimon=100 mode=1" >> /etc/modprobe.conf 
  53.  
  54. #vim /etc/rc.d/rc.local 
  55. echo "ifenslave bond0 eth0 eth1" >> /etc/rc.d/rc.local 
  56. rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0.txt 
  57.  
  58. #重启网络 
  59. ifconfig bond0 up
  60. service network restart 
  61. echo "double network cards bond complete!" 

搞定,给个执行权限 chmod +x bond.sh

执行./bond.sh,看效果

shell脚本自动配置双网卡绑定_第1张图片

---------------------------------华丽的分割线-----------------------------------

[root@nao tmp]# ifconfig
bond0     Link encap:Ethernet  HWaddr 08:00:27:6E:72:FE 
          inet addr:10.1.1.250  Bcast:10.1.1.255  Mask:255.255.255.0
          inet6 addr: fe80::a00:27ff:fe6e:72fe/64 Scope:Link
          UP BROADCAST RUNNING MASTER MULTICAST  MTU:1500  Metric:1
          RX packets:10954 errors:0 dropped:0 overruns:0 frame:0
          TX packets:488 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:944060 (921.9 KiB)  TX bytes:85171 (83.1 KiB)

eth0      Link encap:Ethernet  HWaddr 08:00:27:6E:72:FE 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:5598 errors:0 dropped:0 overruns:0 frame:0
          TX packets:422 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:487797 (476.3 KiB)  TX bytes:74268 (72.5 KiB)

eth1      Link encap:Ethernet  HWaddr 08:00:27:6E:72:FE 
          UP BROADCAST RUNNING SLAVE MULTICAST  MTU:1500  Metric:1
          RX packets:5361 errors:0 dropped:0 overruns:0 frame:0
          TX packets:75 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:456563 (445.8 KiB)  TX bytes:12305 (12.0 KiB)

lo        Link encap:Local Loopback 
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:1793 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1793 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:2907560 (2.7 MiB)  TX bytes:2907560 (2.7 MiB)

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

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

Slave Interface: eth0
MII Status: up
Link Failure Count: 0
Permanent HW addr: 08:00:27:6e:72:fe

Slave Interface: eth1
MII Status: up
Link Failure Count: 0
Permanent HW addr: 08:00:27:d2:f3:dc

OK,效果已呈现,最熟练的时候可以两分钟搞定,现在两秒搞定,这就是速度,这就是效率。