Script to manage bridge with multiple TAP interfaces.
Posted: 21. Nov 2008, 17:53
I created the following "vboxbridge" script that is able to manage a bridge with multiple tap interfaces on my Ubuntu 8.04 host system. It works both with static and dynamic IPs and even with WiCD when you turn the "Automatically reconnect on connection loss" option off. The bridge is only created once and automatically removed after the last tap interface has been stopped.
You create a tap interface with: vboxbridge eth0 tap1 start
To remove the interface: vboxbridge eth0 tap1 stop
(create as many taps you like tap111, tap112)
The script works excellent when used manually (with sudo), BUT:
I tried to run the script automatically via the "Setup Application" and "Terminate Application" feature under Host Interface Settings, but it does not work. I tried both with "sudo" and run as root (chmod +s), all kinds of quotes and 'ยด`, but I always an error message stating that the host interface could not be initialized.
I would appreciate any help to get this going.
You create a tap interface with: vboxbridge eth0 tap1 start
To remove the interface: vboxbridge eth0 tap1 stop
(create as many taps you like tap111, tap112)
The script works excellent when used manually (with sudo), BUT:
I tried to run the script automatically via the "Setup Application" and "Terminate Application" feature under Host Interface Settings, but it does not work. I tried both with "sudo" and run as root (chmod +s), all kinds of quotes and 'ยด`, but I always an error message stating that the host interface could not be initialized.
I would appreciate any help to get this going.
Code: Select all
#!/bin/sh
#Put your username here
USERNAME=mark
TAP_INTERFACE="$2"
HOST_INTERFACE="$1"
BRIDGE_INTERFACE="br0"
TAP_COUNT=`ifconfig | grep -c tap`
case "$3" in
start)
if [ `ifconfig | grep -c $TAP_INTERFACE` = 0 ]; then
#create the bridge if it does not exist
if [ `ifconfig | grep -c $BRIDGE_INTERFACE` = 0 ]; then
brctl addbr $BRIDGE_INTERFACE
#check if we are using DHCP and retrieve configuration if IP is static
if [ `ps ax | grep -c "dhclient $HOST_INTERFACE"` = 2 ]; then
ifconfig $HOST_INTERFACE 0.0.0.0 promisc
brctl addif $BRIDGE_INTERFACE $HOST_INTERFACE
dhclient $BRIDGE_INTERFACE
else
IP_ADDRESS=`ifconfig $HOST_INTERFACE | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
BROADCAST_ADDRESS=`ifconfig $HOST_INTERFACE | grep 'inet addr:' | cut -d: -f3 | awk '{ print $1}'`
SUBNET_MASK=`ifconfig $HOST_INTERFACE | grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'`
DEFAULT_GATEWAY=`route -n |tail -n1|cut -d' ' -f10`
ifconfig $HOST_INTERFACE 0.0.0.0 promisc
brctl addif $BRIDGE_INTERFACE $HOST_INTERFACE
ifconfig $BRIDGE_INTERFACE $IP_ADDRESS netmask $SUBNET_MASK broadcast $BROADCAST_ADDRESS
route add default gw $DEFAULT_GATEWAY
fi
fi
#add tap interface to bridge
tunctl -t $TAP_INTERFACE -u $USERNAME
brctl addif $BRIDGE_INTERFACE $TAP_INTERFACE
ifconfig $TAP_INTERFACE up
chmod 0666 /dev/net/tun
else
echo "Interface $TAP_INTERFACE already configured"
fi
;;
stop)
if [ `ifconfig | grep -c $TAP_INTERFACE` = 0 ]; then
echo "Interface $TAP_INTERFACE does no exist"
else
#shut down tap interface and remove it from bridge
ifconfig $TAP_INTERFACE down
brctl delif $BRIDGE_INTERFACE $TAP_INTERFACE
tunctl -d $TAP_INTERFACE
#we remove the bridge if this was the last tap interface
if [ $TAP_COUNT = 1 ]; then
brctl delif $BRIDGE_INTERFACE $HOST_INTERFACE
ifconfig $HOST_INTERFACE 0.0.0.0 -promisc
#check if we are using DHCP and retrieve configuration if IP is static
if [ `ps ax | grep -c "dhclient $BRIDGE_INTERFACE"` = 2 ]; then
ifconfig $BRIDGE_INTERFACE down
brctl delbr $BRIDGE_INTERFACE
dhclient $HOST_INTERFACE
else
IP_ADDRESS=`ifconfig $BRIDGE_INTERFACE | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'`
BROADCAST_ADDRESS=`ifconfig $BRIDGE_INTERFACE | grep 'inet addr:' | cut -d: -f3 | awk '{ print $1}'`
SUBNET_MASK=`ifconfig $BRIDGE_INTERFACE | grep 'inet addr:' | cut -d: -f4 | awk '{ print $1}'`
DEFAULT_GATEWAY=`route -n |tail -n1|cut -d' ' -f10`
ifconfig $BRIDGE_INTERFACE down
brctl delbr $BRIDGE_INTERFACE
ifconfig $HOST_INTERFACE $IP_ADDRESS netmask $SUBNET_MASK broadcast $BROADCAST_ADDRESS
route add default gw $DEFAULT_GATEWAY
fi
fi
fi
;;
*)
echo "Usage: vboxbridge hostinterface tapinterface {start|stop}" >&2
exit 1
;;
esac