From e55c95bdd7cd5382037052542d37e63b846e31cd Mon Sep 17 00:00:00 2001 From: Michael Gebetsroither Date: Wed, 3 Oct 2007 20:03:17 +0200 Subject: [PATCH] grml-addtun: added -h and bridge add/remove handling --- sbin/grml-addtun | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/sbin/grml-addtun b/sbin/grml-addtun index af1b810..7c641f1 100755 --- a/sbin/grml-addtun +++ b/sbin/grml-addtun @@ -10,23 +10,29 @@ set -e #set -x +PN_="`basename $0`" OPT_DEL_='false' OPT_USER_='' OPT_GROUP_='' OPT_BRIDGE_='' -while getopts "du:g:b:h" opt; do - case "$opt" in - d) OPT_DEL_='true' ;; - u) OPT_USER_="$OPTARG" ;; - g) OPT_GROUP_="$OPTARG" ;; - b) OPT_BRIDGE_="$OPTARG" ;; - h) printUsage; exit 0 ;; - ?) printUsage; exit 1 ;; - esac -done -shift $(($OPTIND - 1)) +function printUsage() +{ + cat < ... + +$PN_ creates persistent tun/tap devices and optionally add them to a bridge + +OPTIONS: + -d delete the given tun devices and remove them from the bridge if given + -u this user should be able to use the tun device + -g this group should be able to use the tun device + -b if given, all tun/tap devices are added/removed from the bridge + bridge is created if not allready existent + -h this help +EOT +} function fromCmdline() { @@ -49,6 +55,7 @@ function createTun() if [[ $OPT_GROUP_ != '' ]]; then args_="$args_ -u $OPT_GROUP_"; fi tunctl -t "$1" $args_ if [[ $OPT_BRIDGE_ != '' ]]; then + brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_" brctl addif "$OPT_BRIDGE_" "$1" fi } @@ -61,10 +68,33 @@ function trashTun() tunctl -d "$1" } + +## +# MAIN +## + +while getopts "du:g:b:h" opt; do + case "$opt" in + d) OPT_DEL_='true' ;; + u) OPT_USER_="$OPTARG" ;; + g) OPT_GROUP_="$OPTARG" ;; + b) OPT_BRIDGE_="$OPTARG" ;; + h) printUsage; exit 0 ;; + ?) printUsage; exit 1 ;; + esac +done +shift $(($OPTIND - 1)) + if [[ $OPT_DEL_ == 'false' ]]; then + if [[ $OPT_BRIDGE_ != '' ]]; then + brctl showmacs "$OPT_BRIDGE_" &>/dev/null || brctl addbr "$OPT_BRIDGE_" + fi fromCmdline "createTun" "$@" else fromCmdline "trashTun" "$@" + if [[ $OPT_BRIDGE_ != '' ]]; then + brctl showmacs "$OPT_BRIDGE_" &>/dev/null && brctl delbr "$OPT_BRIDGE_" + fi fi # vim: filetype=sh -- 2.1.4