testing: Switch to Debian based guest images
[strongswan.git] / testing / scripts / function.sh
1 #!/bin/bash
2 # provides some general-purpose script functions
3 #
4 # Copyright (C) 2004 Eric Marchionni, Patrik Rayo
5 # Zuercher Hochschule Winterthur
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16
17
18 ############################################
19 # print output in color
20 #
21
22 function cecho {
23 echo -e "\033[1;31m$1\033[0m"
24 }
25 function cgecho {
26 echo -e "\033[1;32m$1\033[0m"
27 }
28
29 function cecho-n {
30 echo -en "\033[1;31m$1\033[0m"
31 }
32
33
34 #############################################
35 # output all args to stderr and exit with
36 # return code 1
37 #
38
39 die() {
40 echo $* 1>&2
41 exit 1
42 }
43
44 #############################################
45 # search and replace strings throughout a
46 # whole directory
47 #
48
49 function searchandreplace {
50
51 SEARCHSTRING="$1"
52 REPLACESTRING="$2"
53 DESTDIR="$3"
54
55 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
56
57
58 ###########################################
59 # search and replace in each found file the
60 # given string
61 #
62
63 for eachfoundfile in `find $DESTDIR -type f`
64 do
65 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
66 done
67
68 }
69
70 #############################################
71 # add a bridge
72 #
73
74 function umlbr_add {
75 brctl addbr "umlbr$1"
76 brctl setfd "umlbr$1" 0
77 brctl setageing "umlbr$1" 3600
78 brctl stp "umlbr$1" off
79 ifconfig "umlbr$1" "$2" netmask "$3" up
80 }
81
82 #############################################
83 # delete a bridge
84 #
85
86 function umlbr_del {
87 ifconfig "umlbr$1" down &> /dev/null 2>&1
88 brctl delbr "umlbr$1" &> /dev/null 2>&1
89 }
90
91 #############################################
92 # add a tap interface to a bridge
93 #
94
95 function umlbr_add_tap {
96 tunctl -t "tap$1_$2" &> /dev/null 2>&1
97 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
98 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
99 cecho-n "$2.."
100 }
101
102 #############################################
103 # delete a tap interface from a bridge
104 #
105
106 function umlbr_del_tap {
107 ifconfig "umlbr$2" down &> /dev/null 2>&1
108 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
109 tunctl -d "tap$1_$2" &> /dev/null 2>&1
110 cecho-n "$2.."
111 }
112