added grml-addtun
[grml-network.git] / sbin / grml-addtun
1 #!/bin/bash
2 # Filename:      grml-addtun
3 # Purpose:       Program to create tun/tap devices and add them to a bridge
4 # Authors:       Michael Gebetsroither <gebi@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8 # grml-addtun [OPTIONS] <tun0> <tun1> ...
9
10 set -e
11 #set -x
12
13 OPT_DEL_='false'
14 OPT_USER_=''
15 OPT_GROUP_=''
16 OPT_BRIDGE_=''
17
18 while getopts "du:g:b:h" opt; do
19     case "$opt" in
20         d) OPT_DEL_='true' ;;
21         u) OPT_USER_="$OPTARG" ;;
22         g) OPT_GROUP_="$OPTARG" ;;
23         b) OPT_BRIDGE_="$OPTARG" ;;
24         h) printUsage; exit 0 ;;
25         ?) printUsage; exit 1 ;;
26     esac
27 done
28 shift $(($OPTIND - 1))
29
30
31 function fromCmdline()
32 {
33     local action_="$1"
34     shift
35
36     while (( $# != 0 )); do
37         case "$1" in
38             "") continue ;;
39         esac
40         $action_ "$1"
41         shift
42     done
43 }
44
45 function createTun()
46 {
47     local args_=''
48     if [[ $OPT_USER_ != '' ]]; then args_="$args_ -u $OPT_USER_"; fi
49     if [[ $OPT_GROUP_ != '' ]]; then args_="$args_ -u $OPT_GROUP_"; fi
50     tunctl -t "$1" $args_
51     if [[ $OPT_BRIDGE_ != '' ]]; then
52         brctl addif "$OPT_BRIDGE_" "$1"
53     fi
54 }
55
56 function trashTun()
57 {
58     if [[ $OPT_BRIDGE_ != '' ]]; then
59         brctl delif "$OPT_BRIDGE_" "$1"
60     fi
61     tunctl -d "$1"
62 }
63
64 if [[ $OPT_DEL_ == 'false' ]]; then
65     fromCmdline "createTun" "$@"
66 else
67     fromCmdline "trashTun" "$@"
68 fi
69
70 # vim: filetype=sh