Script to manage bridge with multiple TAP interfaces.

Discussions related to using VirtualBox on Linux hosts.

Script to manage bridge with multiple TAP interfaces.

Postby mark_orion » 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.

Code: Select all   Expand viewCollapse view
#!/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

mark_orion
 
Posts: 33
Joined: 4. Nov 2008, 17:16

Postby Sasquatch » 22. Nov 2008, 01:57

Since you would run the script from a terminal with sudo, it will ask for a password, but from a GUI, it can't do that with sudo. Use gksudo instead, so you get a GUI for your password. So for the start and stop commands, use /usr/bin/gksudo /path/to/script.
Read the Forum Posting Guide before opening a topic.
VirtualBox FAQ: Check this before asking questions.
Online User Manual: A must read if you want to know what we're talking about.
Howto: Install Linux Guest Additions
Howto: Use Shared Folders on Linux Guest
See the Tutorials and FAQ section at the top of the Forum for more guides.
Try searching the forums first with Google and add the site filter for this forum.
E.g. install guest additions site:forums.virtualbox.org

Retired from this Forum since OSSO introduction.
Sasquatch
Volunteer
 
Posts: 17800
Joined: 17. Mar 2008, 13:41
Location: /dev/random
Primary OS: Debian other
VBox Version: PUEL
Guest OSses: Windows XP, Windows 7, Linux

Re: Script to manage bridge with multiple TAP interfaces.

Postby magloca » 30. Apr 2009, 11:21

You, sir, are a hero! I was going insane over this bridging stuff and this script seems to have cut my suffering short... Thanks for sharing.
magloca
 
Posts: 1
Joined: 30. Apr 2009, 11:17
Primary OS: Ubuntu 8.04
VBox Version: PUEL
Guest OSses: RHEL5


Return to VirtualBox on Linux Hosts

Who is online

Users browsing this forum: No registered users and 11 guests