天天看點

shell自動配置bond

#!/bin/bash
#自動配置bond
#by liuq
MODPROBE="/sbin/modprobe"
cd /etc/sysconfig/network-scripts 
if [ -f ifcfg-eth0 ]
then
	cp ifcfg-eth0 /tmp/ifcfg-eth0.bak
	cp ifcfg-eth1 /tmp/ifcfg-eth1.bak
	ETH0=eth0
	ETH1=eth1
elif [ -f ifcfg-em0 ]
then
	cp ifcfg-em1 /tmp/ifcfg-em1.bak
	cp ifcfg-em2 /tmp/ifcfg-em2.bak
	ETH0=em1
	ETH1=em2
fi
bond_add()
{
echo -n "Please input your ip address for bond0:" 
read bond0_ipaddr
echo -n "Please input your ip addfress for netmask:"
read bond0_netmask
echo -n "Please input your ip address for gateway:"
read bond0_gateway
}
bond_add
cat >>ifcfg-bond0<<EOF
DEVICE="bond0"
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
NM_CONTROLLED=no
IPADDR=$bond0_ipaddr
NETMASK=$bond0_netmask
GATEWAY=$bond0_gateway
BONDING_OPTS="mode=1 miimon=100"
USERCTL=no
EOF

cat >ifcfg-${ETH0}<<EOF
DEVICE=${ETH0}
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
MASTER=bond0
SLAVE=yes
DEFROUTE=yes
IPV6INIT=no
USERCTL=no
EOF

cat >ifcfg-${ETH1}<<EOF
DEVICE=${ETH0}
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=static
MASTER=bond0
SLAVE=yes
DEFROUTE=yes
IPV6INIT=no
USERCTL=no
EOF


echo "ifenslave bond0 ${ETH}0 ${ETH}1">>/etc/rc.local
echo "route add -net 0.0.0.0/0 gw $bond0_gateway dev bond0">>/etc/rc.local

echo "alias bond0 bonding ">>/etc/modprobe.d/dist.conf
echo "options bonding mode=1 miimon=100">>/etc/modprobe.d/dist.conf

$MODPROBE bonding

/etc/init.d/network restart

echo "-----------------------bond0主備狀态------------------"
more /proc/net/bonding/bond0