#!/bin/bash export PATH="$PATH:/sbin:/usr/sbin" usage () { echo -e "Usage:\t$0 -a -b [ -u ]" echo -e "\t\t-d -b -t " exit 1 } tap2brigde () { local bridge_id=$1 local user_name=$2 tap_id=$( tunctl -u "$user_name" -b ) ip link set $tap_id up sleep 0.5 brctl addif $bridge_id $tap_id echo $tap_id } removetap () { local bridge_id=$1 local tap_id=$2 brctl delif $bridge_id $tap_id sleep 0.5 tunctl -d $tap_id } [ $# -eq 0 ] && usage # parse options while getopts ":adb:t:u:" opt; do case $opt in a) addtap=1 ;; d) deltap=1 ;; b) bridge_id="$OPTARG" ;; t) tap_id="$OPTARG" ;; u) owner="$OPTARG" ;; \?) usage ;; :) echo "Option -$OPTARG requires an argument." >&2 usage ;; esac done [ -z "$owner" ] && owner="root" if [ -n "$addtap" ]; then tap2brigde "$bridge_id" "$owner" elif [ -n "$deltap" ]; then removetap "$bridge_id" "$tap_id" fi