手動一枚一枚編輯 ifcfg 檔案來建網橋,機器一多就吃不消。下面這支 Shell 腳本把流程互動化:讓你輸入網橋名稱、要加入的實體網卡、以及 IP 與遮罩,然後自動產生所有設定檔。已在 CentOS 5 上測試通過。
#!/bin/sh
##############################################################
#
# function: create linux bridge, CentOS 5 test pass.
# user: chenjian
# date : 2011-06-30 ^_^.
#
##############################################################
NETWORK_DIR=/etc/sysconfig/network-scripts
if ! `rpm -qi bridge-utils > /dev/null`;
then
echo "bridge-utils package not install."
fi
rm -rf $NETWORK_DIR/ifcfg-br*
read -p "Input your bridge name,ex br0: "
if [ `echo $REPLY|wc -L` -eq 0 ];
then
REPLY=br0
fi
BRIDGE=$REPLY
ETHERNET_ALL=$( ls -l $NETWORK_DIR|awk -F'-' '/ifcfg-eth/{print $8}')
while [ `echo $REPLY|wc -w` -lt 2 ];
do
read -p "Input you need to use the network adapter, separated by a space. current cards(`echo $ETHERNET_ALL`),chose two: "
done
ETHERNET=$REPLY
for i in `seq 1 3`;
do
read -p "Input the bridge ip address and netmask. separated by a space: "
if [ `echo $REPLY|wc -L` -eq 0 ];
then
echo "Input empty. this bridage will no ip address. "
REPLY="0.0.0.0 0.0.0.0"
sleep 3
break
elif `echo $REPLY|grep -v '[0-9]\.' > /dev/null`;
then
echo "Use only numbers and points."
elif [ `echo $REPLY|awk '{print $2}'|wc -L` -eq 0]
then
echo "Input errors, exit."
exit 2
else
break
fi
done
IP=`echo $REPLY|awk '{print $1}'`
NETMASK=`echo $REPLY|awk '{print $2}'`
brctl addbr $BRIDGE
cat > $NETWORK_DIR/ifcfg-$BRIDGE <<-EOF
DEVICE=$BRIDGE
BOOTPROTO=static
ONBOOT=yes
NETMASK=$NETMASK
IPADDR=$IP
TYPE=Bridge
EOF
echo -------------------------------------
echo "Bridge info "
echo -------------------------------------
cat $NETWORK_DIR/ifcfg-$BRIDGE
echo ""
for i in `echo $ETHERNET`;
do
MAC=$(ifconfig $i|awk '/HWaddr/{print $NF }')
cat > $NETWORK_DIR/ifcfg-$i <<EOF
DEVICE=$i
HWADDR=$MAC
BOOTPROTO=none
ONBOOT=yes
HOTPLUG=no
TYPE=Ethernet
BRIDGE=$BRIDGE
EOF
echo -------------------------------------
echo "$i info"
echo -------------------------------------
cat $NETWORK_DIR/ifcfg-$i
echo ""
done
echo ""
echo -------------------------------------
echo -e "install successful ,you need to restart your network"
echo -------------------------------------
exit 0;
執行完畢後,記得重啟網路(service network restart)或整機重開,網橋才會真正拉起。