Provide log_action and log_status functions
[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 # output functions
20 #
21
22 export TERM=xterm
23
24 function cecho {
25 echo -e "\033[1;31m$1\033[0m"
26 }
27 function cgecho {
28 echo -e "\033[1;32m$1\033[0m"
29 }
30
31 function cecho-n {
32 echo -en "\033[1;31m$1\033[0m"
33 }
34
35 # log an action
36 # $1 - current action description
37 log_action()
38 {
39 /bin/echo -n "[....] $1 "
40 }
41
42 # log an action status
43 # $1 - exit status of action
44 log_status()
45 {
46 RED=$(tput setaf 1)
47 GREEN=$(tput setaf 2)
48 NORMAL=$(tput op)
49
50 tput hpa 0
51 if [ $1 -eq 0 ]; then
52 /bin/echo -ne "[${GREEN} ok ${NORMAL}"
53 else
54 /bin/echo -ne "[${RED}FAIL${NORMAL}"
55 fi
56 echo
57 }
58
59 #############################################
60 # output all args to stderr and exit with
61 # return code 1
62 #
63
64 die() {
65 echo $* 1>&2
66 exit 1
67 }
68
69 #############################################
70 # search and replace strings throughout a
71 # whole directory
72 #
73
74 function searchandreplace {
75
76 SEARCHSTRING="$1"
77 REPLACESTRING="$2"
78 DESTDIR="$3"
79
80 [ -d "$DESTDIR" ] || die "$DESTDIR is not a directory!"
81
82
83 ###########################################
84 # search and replace in each found file the
85 # given string
86 #
87
88 for eachfoundfile in `find $DESTDIR -type f`
89 do
90 sed -i -e "s/$SEARCHSTRING/$REPLACESTRING/g" "$eachfoundfile"
91 done
92
93 }
94
95 #############################################
96 # add a bridge
97 #
98
99 function umlbr_add {
100 brctl addbr "umlbr$1"
101 brctl setfd "umlbr$1" 0
102 brctl setageing "umlbr$1" 3600
103 brctl stp "umlbr$1" off
104 ifconfig "umlbr$1" "$2" netmask "$3" up
105 }
106
107 #############################################
108 # delete a bridge
109 #
110
111 function umlbr_del {
112 ifconfig "umlbr$1" down &> /dev/null 2>&1
113 brctl delbr "umlbr$1" &> /dev/null 2>&1
114 }
115
116 #############################################
117 # add a tap interface to a bridge
118 #
119
120 function umlbr_add_tap {
121 tunctl -t "tap$1_$2" &> /dev/null 2>&1
122 ifconfig "tap$1_$2" 0.0.0.0 promisc up &> /dev/null 2>&1
123 brctl addif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
124 cecho-n "$2.."
125 }
126
127 #############################################
128 # delete a tap interface from a bridge
129 #
130
131 function umlbr_del_tap {
132 ifconfig "umlbr$2" down &> /dev/null 2>&1
133 brctl delif "umlbr$1" "tap$1_$2" &> /dev/null 2>&1
134 tunctl -d "tap$1_$2" &> /dev/null 2>&1
135 cecho-n "$2.."
136 }
137