為了建立 bridge環境,寫了一 script,來讓它開機時啟動。
但後來也得知 network-script目錄裡的 ifcfg等檔可支援 brige
#!/bin/bash
# fixed 2006/05/22 by cross@ssorc.tw
#
# purpose: set up bridge mode for linux.
#
# 1. first need to set up ifcfg-ethx.
# 2. modprobe bridge
# 3. apt-get install bridge-utils
#
# define external device.
exif=”eth0″
# define internal device.
inif=”eth1″
# define bridge name and ip/mask.
brname=”br0″
brip=”10.1.1.33″
brmask=”255.255.255.0″
# define external gateway ip
gw=”10.1.1.254″
# add brX alias
braliasip=”10.1.1.44 10.1.1.55″
braliasmask=”255.255.255.0″
# source function library
. /etc/rc.d/init.d/functions
# ——- check ifcfg-ethx setting – START line ——-
cd /etc/sysconfig/network-scripts/
ls ifcfg-eth* | sed “s/:.*//” | uniq | cut -d’h’ -f2 |
while read ifdev; do
if [ -f ifcfg-$exif ] && [ -f ifcfg-$inif ]; then
echo “ifdev setting ok!”
else
if [ $ifdev = `echo $exif | cut -d’h’ -f2` ]; then
echo “warning !! only the ifcfg-eth$ifdev”
echo “warning !! plz set up ifcfg-$inif”
echo “”
exit 1
else
echo “warning !! only the ifcfg-eth$ifdev”
echo “warning !! plz set up ifcfg-$exif”
echo “”
exit 1
fi
fi
done
# ——- check ifcfg-ethx setting – END line ——-
# ——- start to set up bridge mode ——-
start() {
echo “starting bridge mode: ”
ifdown $exif
ifdown $inif
brctl addbr $brname
brctl stp $brname off
brctl addif $brname $exif
brctl addif $brname $inif
ifconfig $exif 0.0.0.0 up
ifconfig $inif 0.0.0.0 up
ifconfig $brname $brip netmask $brmask up
nowgw1=`route | grep default | awk ‘{print $2}’`
if [ -z $nowgw1 ]; then
route add default gw $gw
else
:
fi
#
echo 1 > /proc/sys/net/ipv4/ip_forward
# ——- set up br0:XX ——-
if [ -z $braliasip ]; then
:
else
ipcounts=`echo $braliasip | wc -w`
for ((n=1; n<=$ipcounts; n++))
do
ipaddr=`echo $braliasip | awk '{print $'$n'}'`
ipalias=`echo $ipaddr | cut -d'.' -f4`
ifconfig $brname:$ipalias $ipaddr netmask $braliasmask
done
fi
}
stop() {
echo "stopping bridge mode: "
brctl delif $brname $exif
brctl delif $brname $inif
ifconfig $brname down
brctl delbr $brname
ifup $exif
ifup $inif
nowgw2=`route | grep default | awk '{print $2}'`
if [ -z $nowgw2 ]; then
route add default gw $gw dev $exif
else
:
fi
}
status() {
echo "ifocnfig status: "
ifconfig $exif | grep "inet addr"
ifconfig $inif | grep "inet addr"
ifconfig $brname | grep "inet addr"
echo ""
echo "routing status: "
route -n
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
esac
exit $?
留言