[ltp] Network - 2 ethernet cards instead of a hub

Richard Neill linux-thinkpad@linux-thinkpad.org
Sat, 27 Dec 2003 04:37:09 +0000

This is a multi-part message in MIME format.
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit

Thank you - I am *such* a twit! I failed to realise that although the 
ethernet cards that comprise a bridge cannot have IP addresses, the 
bridge itself can do so.

For such use as it may beto anyone, I attach my finished script - it's 
rather ugly, but it does do the job (put it in /etc/init.d).

Best wishes


Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;

# chkconfig: - - -
# description: Starts and stops the ethernet bridge br0 between eth0 and eth1

#This is a script to set up ethernet bridging on espresso.baddiant.org.uk
#It assumes that the network is already configured: eth0 is up and has IP assigned by dhcp; eth1 is not configured.
#It creates the bridge device br0 and merges eth0 and eth1 into it. It then sets this up on the network.

# Source function library.
. /etc/init.d/functions

prog=$"ethernet bridge br0"

start() {
	gprintf "Starting %s: \n" "$prog"

	#Does the bridge br0 already exist?
	if brctl show | grep '^br0' >/dev/null; then
		echo "Sorry, br0 already exists. Try stopping it first."
		exit 1

	#First, get the address we want the bridge to have (i.e the current address of eth0):
	BRIDGE_ADDR=`/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $2}' | grep -oE  '([[:digit:]])+\.([[:digit:]])+\.([[:digit:]])+\.([[:digit:]])+'`
	#Similarly, get the netmask (usually
        BRIDGE_NETMASK=`/sbin/ifconfig eth0 | grep 'inet addr' | awk '{print $4}' | grep -oE  '([[:digit:]])+\.([[:digit:]])+\.([[:digit:]])+\.([[:digit:]])+'`
	#Similarly, get the gateway
	GATEWAY=`/sbin/route -n | grep -E '^0\.0\.0\.0' | awk '{print $2}'`
	echo "Bridge Address will be: $BRIDGE_ADDR with netmask $BRIDGE_NETMASK and gateway $GATEWAY"

	#Now release both eth0 and eth1:
	echo "Stopping eth0 and eth1"
	/sbin/ifconfig eth0 down
	/sbin/ifconfig eth1 down

	#Create a bridge device br0 (with spanning tree protocol off)
	echo "Creating bridge device br0"
	/usr/sbin/brctl addbr br0
	/usr/sbin/brctl stp br0 off

	#Make sure they are both up:
	echo "Starting eth0 and eth1"
	/sbin/ifconfig eth0 up
	/sbin/ifconfig eth1 up

	#Associate eth0 and eth1 with the bridge.
	echo "Adding eth0 and eth1 to bridge br0"
	/usr/sbin/brctl addif br0 eth0
	/usr/sbin/brctl addif br0 eth1

	#Now configure the bridge interface:
	echo "Setting bridge interface IP to $BRIDGE_ADDR and netmask to $BRIDGE_NETMASK"
	/sbin/ifconfig br0 $BRIDGE_ADDR netmask $BRIDGE_NETMASK

	echo "Setting gateway to be $GATEWAY"
	route add default gw $GATEWAY

	#Ugly hack = why does this take so long?
	echo "Waiting 15 seconds for routing to take effect."
	sleep 15

	echo "...Finished"

stop() {
	gprintf "Shutting down %s: \n" "$prog"

	#Does the bridge br0 already exist?
        if brctl show | grep '^br0' >/dev/null ; then

		echo "Stopping the bridge device br0"
		/sbin/ifconfig br0 down

		echo "Removing eth0 and eth1 from bridge"
		/usr/sbin/brctl delif br0 eth0
		/usr/sbin/brctl delif br0 eth1

		echo "Deleting the bridge br0"
		/usr/sbin/brctl delbr br0

		echo "Bridge br0 does not exist - not attempting to stop it."

	echo "Restarting the network as before"
	/sbin/service network restart

	echo "...Finished"

status() {
        if brctl show | grep '^br0' >/dev/null ; then
		gprintf "%s is configured.\n" "$prog"
		gprintf "%s is not configured.\n" "$prog"

# See how we were called.
case "$1" in
	gprintf "Usage: %s {start|stop|status}\n" "$0"
	exit 1