shell脚本自动配置双网卡绑定
配置双网卡绑定是运维的基本工作之一,第一次配置会觉得很稀奇,第十次配置会觉得不过尔尔,第三十次配置可以两分钟搞定,但第一百次配置的时候会有一种吐血的感觉,那怎么办呢?果断懒人原理,写脚本,自动化配置。 www.zhishiwu.com
#!/bin/bash
#double network cards bond
#write by xiaojing.zhao
#2012.11.19
cd /etc/sysconfig/network-scripts
cp ifcfg-eth0 /tmp/ifcfg-eth0.bak
cp ifcfg-eth1 /tmp/ifcfg-eth1.bak
#cp ifcfg-eth0 ifcfg-bond0
#vim eth0
cat ifcfg-eth0 | grep -i hwaddr > ifcfg-eth0.txt
#在HWADDR匹配行前插入SLAVE=yes,以此类推,此处也可以用echo输入来完成
www.zhishiwu.com
sed -i '/HWADDR/ i SLAVE=yes' ifcfg-eth0.txt
sed -i '/SLAVE/ i MASTER=bond0' ifcfg-eth0.txt
sed -i '/MASTER/ i BOOTPROTO=none' ifcfg-eth0.txt
sed -i '/BOOTPROTO/ i DEVICE=eth0' ifcfg-eth0.txt
sed -i '/HWADDR/ a IPV6INIT=no' ifcfg-eth0.txt
sed -i '/IPV6INIT/ a ONBOOT=yes' ifcfg-eth0.txt
sed -i '/ONBOOT/ a TYPE=Ethernet' ifcfg-eth0.txt
sed -i '/TYPE/ a PEERDNS=yes' ifcfg-eth0.txt
sed -i '/PEERDNS/ a USERCTL=no' ifcfg-eth0.txt
cat ifcfg-eth0.txt > ifcfg-eth0
#vim eth1
#在匹配DEVICE行,将(y指所有)0替换成1,即eth0替换为eth1
sed -i '/DEVICE/y/0/1/' ifcfg-eth0.txt
#变量linehwaddr为取出eth1的MAC地址``为通配符
linehwaddr=`cat /etc/sysconfig/network-scripts/ifcfg-eth1 | grep -i hwaddr`
#将eth0的MAC的地址替换为eth1的MAC地址
sed -i '/HWADDR/c/'"$linehwaddr"'' ifcfg-eth0.txt
cat ifcfg-eth0.txt > ifcfg-eth1
#vim bond0
#脚本通用,只需改下列后五个网段,网址,网关,广播地址,子网掩码即可,按实际情况处理
echo "DEVICE=bond0" > ifcfg-bond0
echo "BOOTPROTO=none" >> ifcfg-bond0
echo "IPV6INIT=no" >> ifcfg-bond0
echo "ONBOOT=yes" >> ifcfg-bond0
echo "TYPE=Ethernet" >> ifcfg-bond0
echo "PEERDNS=yes" >> ifcfg-bond0
echo "USERCTL=no" >> ifcfg-bond0
echo "NETMASK=255.255.255.0" >> ifcfg-bond0
echo "IPADDR=10.1.1.250" >> ifcfg-bond0
echo "GATEWAY=10.1.1.1" >> ifcfg-bond0
echo "BROADCAST=10.1.1.255" >> ifcfg-bond0
echo "NETWORK=10.1.1.0" >> ifcfg-bond0
#vim /etc/modprobe.conf
#追加
echo "alias bond0 bonding" >> /etc/modprobe.conf
echo "options bond0 miimon=100 mode=1" >> /etc/modprobe.conf
#vim /etc/rc.d/rc.local
echo "ifenslave bond0 eth0 eth1" >> /etc/rc.d/rc.local
rm -rf /etc/sysconfig/network-scripts/ifcfg-eth0.txt
#重启网络
ifconfig bond0 up
service network restart
echo "double network cards bond complete!"
搞定,给个执行权限 chmod +x bond.sh
执行./bond.sh,看效果
---------------------------------华丽的分割线-----------------------------------
[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,效果已呈现,最熟练的时候可以两分钟搞定,现在两秒搞定,这就是速度,这就是效率。