Debian Codegraph @

scripts/sbin/select-drbl-clients

raw symbols SCIP
1 #!/bin/bash
2 # Author: Steven Shiau <steven _at_ clonezilla org>
3 # License: GPL
4 # Description: To select the DRBL clients and show the results (IP or MAC)
5
6 # Load DRBL setting and functions
7 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
8
9 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
10
11 MAX_DIALOG_HEIGHT="10"
12
13 #
14 ask_by_MAC_or_IP() {
15 # Note!!! We must use common tmp file (ANS_TMP) to pass the result,
16 # we can NOT
17 # just echo result in the end of this function. Otherwise in newer
18 # dialog (FC3), the script will wait for the end of function, then it
19 # shows the result.
20 # There is nothing in the screen when function is called if we just
21 # use echo the result to stdout.
22 ANS_TMP=$1
23 local TMP=`mktemp /tmp/drbl-tmp.XXXXXX`
24 local mac_list_opt1 mac_list_opt2 mac_list_opt3 mac_grp_opt1 mac_grp_opt2 mac_grp_opt3 M ip_addr_list_opt
25 trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
26 # if hardware is set in dhcpd.conf, then we can choose hosts via MAC
27 # example: "hardware ethernet 00:02:e3:53:34:6e;
28 if grep -q -i "^[[:space:]]*hardware ethernet .*;" $DHCPDCONF_DIR/dhcpd.conf; then
29 mac_list_opt1="by_MAC_addr_list"
30 mac_list_opt2="$(rep_whspc_w_udrsc "$msg_set_by_MAC_address_list")"
31 if [ -n "$(LC_ALL=C ls $drbl_syscfg/client-MAC-group-* $drbl_syscfg/MAC-grp-* 2>/dev/null)" ]; then
32 mac_grp_opt1="by_MAC_addr_grp"
33 mac_grp_opt2="$(rep_whspc_w_udrsc "$msg_set_by_MAC_address_grp")"
34 fi
35 fi
36 if [ -n "$(LC_ALL=C ls $drbl_syscfg/client-IP-group-* $drbl_syscfg/IP-grp-* 2>/dev/null)" ]; then
37 ip_grp_opt1="by_IP_addr_grp"
38 ip_grp_opt2="$(rep_whspc_w_udrsc "$msg_set_by_IP_address_grp")"
39 fi
40 $DIA --backtitle "$msg_nchc_free_software_labs" --title "$msg_drbl_title" \
41 --menu "$msg_specify_hosts,$msg_hint_assign_IP_group,$msg_hint_assign_MAC_group,\n$msg_choose_mode:" 0 0 0 \
42 $mac_list_opt1 $mac_list_opt2 \
43 $mac_grp_opt1 $mac_grp_opt2 \
44 "by_IP_addr_list" "$msg_set_by_IP_address_list" \
45 $ip_grp_opt1 $ip_grp_opt2 \
46 2> $TMP
47 retval=$?
48 [ $retval -eq 1 ] && exit 1
49 M="$(cat $TMP)"
50 case "$M" in
51 "by_MAC_addr_list")
52 # HOST_MAC_TABLE is not created by drblpush, we have to create it.
53 gen_HOST_MAC_TABLE
54 select_hosts_by_MAC $ANS_TMP
55 ;;
56 "by_MAC_addr_grp")
57 # HOST_MAC_TABLE is not created by drblpush, we have to create it.
58 gen_HOST_MAC_TABLE
59 select_hosts_by_MAC_GRP $ANS_TMP
60 ;;
61 "by_IP_addr_list")
62 select_hosts_by_IP $ANS_TMP
63 ;;
64 "by_IP_addr_grp")
65 select_hosts_by_IP_GRP $ANS_TMP
66 ;;
67 esac
68 [ -f "$TMP" ] && rm -f $TMP
69 return 0
70 }
71 #
72 select_hosts_by_IP() {
73 # Note!!! We must use common tmp file (ANS_TMP) to pass the result,
74 # we can NOT
75 # just echo result in the end of this function. Otherwise in newer
76 # dialog (FC3), the script will wait for the end of function, then it
77 # shows the result.
78 # There is nothing in the screen when function is called if we just
79 # use echo the result to stdout.
80 ANS_TMP=$1
81 local TMP=`mktemp /tmp/ocs.XXXXXX`
82 trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
83 local numhosts=0
84 local IP_list host_grp hostinfo igrp list_height
85 # list the nodes
86 awk -F" " '!/^#/ {print $1, $2, "off"}' $IP_HOST_TABLE >> $TMP
87 IP_list="$(cat $TMP)"
88
89 numhosts="$(echo $IP_list | grep -o off | wc -w)"
90 #numhosts=$((numhosts+3))
91 if [ $numhosts -gt 0 ]; then
92 if [ $numhosts -lt $MAX_DIALOG_HEIGHT ]; then
93 list_height=$numhosts
94 else
95 list_height=$MAX_DIALOG_HEIGHT
96 fi
97 $DIA \
98 --separate-output \
99 --backtitle "$msg_nchc_free_software_labs" \
100 --title "$msg_drbl_title" \
101 --checklist "$msg_specify_hosts ($msg_press_space_to_mark_selection):" 20 70 \
102 $list_height $IP_list 2> $TMP
103 retval=$?
104 [ $retval -eq 1 ] && exit 1
105 # Convert the selected group to IP lists
106 if grep -qE "(client-IP-group|IP-grp)" $TMP; then
107 selected_grp="$(grep -Eo "(client-IP-group-.*|IP-grp-.*)" $TMP)"
108 for i in $selected_grp; do
109 LC_ALL=C perl -p -i -e "s/$i//g" $TMP
110 cat $drbl_syscfg/$i >> $TMP
111 done
112 fi
113 target_hosts="$(cat $TMP)"
114 else
115 echo "No hosts in $drblroot!!!... Abort!"
116 exit 1
117 fi
118 echo $target_hosts > $ANS_TMP
119 [ -f "$TMP" ] && rm -f $TMP
120 return 0
121 } # end of select_hosts_by_IP
122 #
123 select_hosts_by_IP_GRP() {
124 # Note!!! We must use common tmp file (ANS_TMP) to pass the result,
125 # we can NOT
126 # just echo result in the end of this function. Otherwise in newer
127 # dialog (FC3), the script will wait for the end of function, then it
128 # shows the result.
129 # There is nothing in the screen when function is called if we just
130 # use echo the result to stdout.
131 ANS_TMP=$1
132 local TMP=`mktemp /tmp/ocs.XXXXXX`
133 trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
134 local hostlist=""
135 local numhosts=0
136 local IP_list host_grp hostinfo igrp list_height numlist
137 # Part I: list the IP group
138 numlist=0
139 for igrp in $drbl_syscfg/client-IP-group-* $drbl_syscfg/IP-grp-*; do
140 [ ! -e "$igrp" ] && continue
141 host_grp="$(basename $igrp)"
142 hostinfo="$(head -n 2 $igrp)"
143 hostinfo="$(echo $hostinfo | tr " " "_")..."
144 hostlist="$hostlist $host_grp $hostinfo off"
145 numlist="$((numlist+1))"
146 done
147 echo $hostlist > $TMP
148
149 IP_list="$(cat $TMP)"
150 if [ $numlist -gt 0 ]; then
151 if [ $numlist -lt $MAX_DIALOG_HEIGHT ]; then
152 list_height=$numlist
153 else
154 list_height=$MAX_DIALOG_HEIGHT
155 fi
156 $DIA \
157 --separate-output \
158 --backtitle "$msg_nchc_free_software_labs" \
159 --title "$msg_drbl_title" \
160 --checklist "$msg_specify_hosts ($msg_press_space_to_mark_selection):" 20 70 \
161 $list_height $IP_list 2> $TMP
162 retval=$?
163 [ $retval -eq 1 ] && exit 1
164 # Convert the selected group to IP lists
165 if grep -qE "(client-IP-group|IP-grp)" $TMP; then
166 selected_grp="$(grep -Eo "(client-IP-group-.*|IP-grp-.*)" $TMP)"
167 for i in $selected_grp; do
168 perl -p -i -e "s/$i//g" $TMP
169 cat $drbl_syscfg/$i >> $TMP
170 done
171 fi
172 target_hosts="$(cat $TMP)"
173 else
174 echo "No hosts in $drblroot!!!... Abort!"
175 exit 1
176 fi
177 echo $target_hosts > $ANS_TMP
178 [ -f "$TMP" ] && rm -f $TMP
179 return 0
180 } # select_hosts_by_IP_GRP
181
182 gen_HOST_MAC_TABLE() {
183 # host fc3-101 {
184 # hardware ethernet 00:02:e3:53:34:60;
185 # fixed-address 192.168.232.1;
186 # }
187 # clean the IP-MAC table
188 [ ! -f $HOST_MAC_TABLE ] && rm -f $HOST_MAC_TABLE
189 # To get the hostname - IP table.
190 parse_dhcpd_conf $HOST_MAC_TABLE
191 }
192 #
193 select_hosts_by_MAC() {
194 # Note!!! We must use common tmp file (ANS_TMP) to pass the result,
195 # we can NOT
196 # just echo result in the end of this function. Otherwise in newer
197 # dialog (FC3), the script will wait for the end of function, then it
198 # shows the result.
199 # There is nothing in the screen when function is called if we just
200 # use echo the result to stdout.
201 local TMP=`mktemp /tmp/drbl-mac-tmp.XXXXXX`
202 local MAC_list host_grp hostinfo igrp list_height
203 trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
204 # list the nodes
205 awk -F" " '!/^#/ {print $3, $1"("$2")", "off"}' $HOST_MAC_TABLE >> $TMP
206 MAC_list="$(cat $TMP)"
207
208 numhosts=$(echo $MAC_list | grep -o off | wc -w)
209 if [ $numhosts -gt 0 ]; then
210 if [ $numhosts -lt $MAX_DIALOG_HEIGHT ]; then
211 list_height=$numhosts
212 else
213 list_height=$MAX_DIALOG_HEIGHT
214 fi
215 $DIA \
216 --separate-output \
217 --backtitle "$msg_nchc_free_software_labs" \
218 --title "$msg_drbl_title" \
219 --checklist "$msg_specify_hosts ($msg_press_space_to_mark_selection):" 20 70\
220 $list_height $MAC_list 2> $TMP
221 retval=$?
222 [ $retval -eq 1 ] && exit 1
223 # Convert the selected group to MAC lists
224 if grep -qE "(client-MAC-group|MAC-grp)" $TMP; then
225 selected_grp="$(grep -Eo "(client-MAC-group-.*|MAC-grp-.*)" $TMP)"
226 for i in $selected_grp; do
227 perl -p -i -e "s/$i//g" $TMP
228 cat $drbl_syscfg/$i >> $TMP
229 done
230 fi
231 target_hosts=$(cat $TMP)
232 else
233 echo "No MAC address in dhcpd.conf! Abort! "
234 exit 1
235 fi
236
237 echo $target_hosts > $ANS_TMP
238 [ -f "$TMP" ] && rm -f $TMP
239 return 0
240 } # end of select_hosts_by_MAC
241 #
242 select_hosts_by_MAC_GRP() {
243 # Note!!! We must use common tmp file (ANS_TMP) to pass the result,
244 # we can NOT
245 # just echo result in the end of this function. Otherwise in newer
246 # dialog (FC3), the script will wait for the end of function, then it
247 # shows the result.
248 # There is nothing in the screen when function is called if we just
249 # use echo the result to stdout.
250 local TMP=`mktemp /tmp/drbl-mac-tmp.XXXXXX`
251 local MAC_list host_grp hostinfo igrp list_height hostlist
252 trap "[ -f "$TMP" ] && rm -f $TMP" HUP INT QUIT TERM EXIT
253 hostlist=""
254 numlist=0
255 # list the IP group
256 for igrp in $drbl_syscfg/client-MAC-group-* $drbl_syscfg/MAC-grp-*; do
257 [ ! -e "$igrp" ] && continue
258 host_grp="$(basename $igrp)"
259 hostinfo="$(head -n 2 $igrp)"
260 hostinfo="$(echo $hostinfo | tr " " "_")..."
261 hostlist="$hostlist $host_grp $hostinfo off"
262 numlist="$((numlist+1))"
263 done
264 echo $hostlist > $TMP
265 MAC_list="$(cat $TMP)"
266 if [ $numlist -gt 0 ]; then
267 if [ $numlist -lt $MAX_DIALOG_HEIGHT ]; then
268 list_height=$numlist
269 else
270 list_height=$MAX_DIALOG_HEIGHT
271 fi
272 $DIA \
273 --separate-output \
274 --backtitle "$msg_nchc_free_software_labs" \
275 --title "$msg_drbl_title" \
276 --checklist "$msg_specify_hosts ($msg_press_space_to_mark_selection):" 20 70\
277 $list_height $MAC_list 2> $TMP
278 retval=$?
279 [ $retval -eq 1 ] && exit 1
280 # Convert the selected group to MAC lists
281 if grep -qE "(client-MAC-group|MAC-grp)" $TMP; then
282 selected_grp="$(grep -Eo "(client-MAC-group-.*|MAC-grp-.*)" $TMP)"
283 for i in $selected_grp; do
284 perl -p -i -e "s/$i//g" $TMP
285 cat $drbl_syscfg/$i >> $TMP
286 done
287 fi
288 target_hosts=$(cat $TMP)
289 else
290 echo "No MAC address in dhcpd.conf! Abort! "
291 exit 1
292 fi
293
294 echo $target_hosts > $ANS_TMP
295 [ -f "$TMP" ] && rm -f $TMP
296 return 0
297 } # end of select_hosts_by_MAC_GRP
298
299 usage() {
300 echo "Description:"
301 echo "Select DRBL clients by IP or MAC address"
302 echo "Usage: `basename $0` [Options] RESULT_FILE"
303 echo "Options:"
304 language_help_prompt_by_idx_no
305 language_help_prompt_by_idx_name
306 dialog_like_prog_help_prompt
307 echo "RESULT_FILE The file to store the selected hosts"
308 }
309
310 # option
311 while [ $# -gt 0 ]; do
312 case "$1" in
313 -l|--language)
314 shift;
315 if [ -z "$(echo $1 |grep ^-.)" ]; then
316 # skip the -xx option, in case
317 language_opt="$1"
318 fi
319 shift ;;
320 -d0|--dialog)
321 DIA="dialog"
322 shift;;
323 -d1|--Xdialog)
324 DIA="Xdialog"
325 shift;;
326 -d2|--whiptail)
327 DIA="whiptail"
328 shift;;
329 -d3|--gdialog)
330 DIA="gdialog"
331 shift;;
332 -d4|--kdialog)
333 DIA="kdialog"
334 shift;;
335 -*) echo "${0}: ${1}: invalid option" >&2
336 usage >& 2
337 exit 2 ;;
338 *) break ;;
339 esac
340 done
341
342 #
343 HOST_FILE=$1
344 [ -z "$HOST_FILE" ] && echo "You must specify the target file!!! Program terminated!!!" && exit 1
345
346 #
347 ask_and_load_lang_set $language_opt
348
349 # check DIA
350 check_DIA_set_ESC $DIA
351
352 # Note!!! We must use common tmp file (ANS_TMP) to pass the result,
353 # we can NOT
354 # just echo result in the end of this function. Otherwise in newer
355 # dialog (FC3), the script will wait for the end of function, then it
356 # shows the result.
357 # There is nothing in the screen when function is called if we just
358 # use echo the result to stdout.
359 ANS_TMP=`mktemp /tmp/mac_ip_ans.XXXXXX`
360 trap "[ -f "$ANS_TMP" ] && rm -f $ANS_TMP" HUP INT QUIT TERM EXIT
361 ask_by_MAC_or_IP $ANS_TMP
362 target_hosts="$(cat $ANS_TMP)"
363 echo $target_hosts > $HOST_FILE
364 [ -f "$ANS_TMP" ] && rm -f $ANS_TMP
365 exit 0