X-Git-Url: http://git.grml.org/?a=blobdiff_plain;f=sbin%2Fgrml-addtun;h=3b6d5b97b8a8dc8b34eedad44c3f88d89139e5f9;hb=6d0d35b2cdeeb64a1dfba054ab32fc6321edcff5;hp=af1b81014a99069c4c65480f66a21b6739cb7c36;hpb=597f86091bc8db5ed31da8daad9dfe58b6ef23c4;p=grml-network.git diff --git a/sbin/grml-addtun b/sbin/grml-addtun index af1b810..3b6d5b9 100755 --- a/sbin/grml-addtun +++ b/sbin/grml-addtun @@ -5,28 +5,33 @@ # Bug-Reports: see http://grml.org/bugs/ # License: This file is licensed under the GPL v2. ################################################################################ -# grml-addtun [OPTIONS] ... 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 with bridge handling + +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() { @@ -61,10 +66,43 @@ function trashTun() tunctl -d "$1" } +function die() +{ + echo "$@" >&2 + exit 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 + tmp_="`brctl showmacs "$OPT_BRIDGE_" |wc -l`" + if (( $tmp_ == 1 )); then + brctl delbr "$OPT_BRIDGE_" + else + die "E: bridge $OPT_BRIDGE_ not empty, not removing" + fi + fi fi # vim: filetype=sh