Debian Codegraph @

sbin/drbl-collect-mac

raw symbols SCIP
1 #!/bin/bash
2 # Author: Blake, Kuo-Lien Huang
3 # License: GPL
4 # Description:
5 # * get the MAC address connected to DRBL server, and seperate them to
6 # different files, according the no. of NICs.
7 # * 2003/08/12
8 # ask user if he would like to detect MAC address on all NICs
9 #
10 # Modified by Steven Shiau <[email protected]> to used in DRBL for Redhat
11 # output files are: macadr-$ethx.txt
12
13 # Load DRBL setting and functions
14 DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}"
15
16 . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions
17
18 #
19 usage() {
20 echo "Collect the MAC addresses of DRBL."
21 echo "Usage: $0 [OPTION] request_ethernet_port_list..."
22 echo "OPTION:"
23 language_help_prompt_by_idx_no
24 echo "-v, --verbose: verbose mode."
25 echo "-n, --no-service-prompt: do not show service restart messages."
26 echo "Ex: $0 eth1 eth2"
27 }
28
29 #
30 server_to_stop_now_debian="$DHCP_SRV_NAME tftpd-hpa"
31 server_to_stop_now_rh="dhcpd xinetd"
32 server_to_stop_now_suse="dhcpd xinetd"
33 #
34 check_if_root
35
36 # default settings:
37 service_restart_prompt="yes"
38
39 # main
40 # Parse command-line options
41 while [ $# -gt 0 ]; do
42 case "$1" in
43 -l|--language)
44 shift
45 if [ -z "$(echo $1 |grep ^-.)" ]; then
46 # skip the -xx option, in case
47 specified_lang="$1"
48 shift
49 fi
50 ;;
51 -n|--no-service-prompt)
52 service_restart_prompt="no"
53 shift;;
54 -v|--verbose)
55 shift
56 VERBOSE=y ;;
57 -q|--quiet) shift ; quiet=-q ;;
58 --help) shift ; do_help=y ;;
59 --) shift ; break ;;
60 *) break ;;
61 esac
62 done
63
64 #
65 eth_port="$*"
66 request_eth_port="$eth_port"
67 echo "request_eth_port:$request_eth_port"
68 if [ -z "$request_eth_port" ]; then
69 usage
70 exit 2
71 fi
72
73 #
74 ask_and_load_lang_set $specified_lang
75
76 # stop inetd, dhcp
77 #echo "Now stop the service dhcp and xinetd if they are running!"
78 if [ -e /etc/debian_version ]; then
79 # Debian
80 for serv_st in $server_to_stop_now_debian; do
81 echo "Stopping $serv_st ..."
82 RETVAL=0
83 /etc/init.d/$serv_st stop
84 RETVAL=$?
85 if [ "$RETVAL" -gt 0 ]; then
86 echo "XXXXXXX XXXXXXX XXXXXXX"
87 echo "Failed to stop service $serv_st !!!"
88 exit $RETVAL
89 fi
90 done
91 elif [ -e /etc/SuSE-release ]; then
92 # SuSE
93 for serv_st in $server_to_stop_now_suse; do
94 # service is running
95 echo "Stopping $serv_st ..."
96 RETVAL=0
97 /etc/init.d/$serv_st stop
98 RETVAL=$?
99 if [ "$RETVAL" -gt 0 ]; then
100 echo "XXXXXXX XXXXXXX XXXXXXX"
101 echo "Failed to stop service $serv_st !!!"
102 exit $RETVAL
103 fi
104 done
105 else
106 # RH-like
107 for serv_st in $server_to_stop_now_rh; do
108 if [ -f /var/lock/subsys/$serv_st ] ; then
109 # service is running
110 echo "Stopping $serv_st ..."
111 RETVAL=0
112 /etc/init.d/$serv_st stop
113 RETVAL=$?
114 if [ "$RETVAL" -gt 0 ]; then
115 echo "XXXXXXX XXXXXXX XXXXXXX"
116 echo "Failed to stop service $serv_st !!!"
117 exit $RETVAL
118 fi
119 fi
120 done
121 fi
122
123 # Clean the old files macadr-ethx.txt
124 if [ "$(ls macadr-eth*.txt 2> /dev/null)" != "" ]; then
125 echo "deleting old macadr-eth*.txt file..."
126 rm -f macadr-eth*.txt
127 fi
128
129 drblmac=$(mktemp -d /tmp/drbl_nic.XXXXXX) || exit 1
130 ## function: mac_collect_status
131 mac_collect_status() {
132 [ -f $drblmac/setup-drbl-auto.all ] && rm -f $drblmac/setup-drbl-auto.all
133 for eth in $request_eth_port; do
134 cat $drblmac/setup-drbl-auto.$eth >> $drblmac/setup-drbl-auto.all
135 done
136 num=$(perl -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.all | sort | uniq | wc | awk '{print $1}')
137 echo "======================================="
138 #perl -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.all | sort | uniq
139 # uniq without sort
140 perl -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.all | perl -ne 'print unless $lines{$_}++'
141 echo "Total: $num"
142 echo "======================================="
143 }
144
145 ## function: stop
146 stop() {
147 killall /usr/sbin/tcpdump
148 }
149
150 ## main
151 for eth in $request_eth_port; do
152 #/usr/sbin/tcpdump -qte -i $eth > $drblmac/setup-drbl-auto.$eth 2> /dev/null &
153 /usr/sbin/tcpdump -teln -i $eth broadcast and port bootpc > $drblmac/setup-drbl-auto.$eth 2> /dev/null &
154 done
155
156 ##
157 echo "$msg_delimiter_star_line"
158 echo "$msg_start_detect_MAC_addresse"
159 echo "$msg_enter_1_or_press_enter_to_view"
160 echo "$msg_enter_2_or_press_q_to_quit"
161 while read CMD; do
162 case "$CMD" in
163 2|[q]|[Q])
164 break
165 ;;
166 *)
167 mac_collect_status
168 echo "$msg_enter_1_or_press_enter_to_view"
169 echo "$msg_enter_2_or_press_q_to_quit"
170 esac
171 done
172
173 ## analyze the mac address
174 for eth in $request_eth_port; do
175 #perl -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.$eth | sort | uniq > $drblmac/macadr-auto-$eth
176 #uniq without sort
177 perl -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.$eth | perl -ne 'print unless $lines{$_}++' > $drblmac/macadr-auto-$eth
178 done
179
180 ethmatch=""
181 for eth in $request_eth_port; do
182 if [ -f $drblmac/macadr-auto-$eth ]; then
183 for ethx in $request_eth_port; do
184 if [ "$ethx" = "$eth" ]; then
185 ethmatch="$eth=$eth $ethmatch"
186 elif [ -f $drblmac/macadr-auto-$ethx ]; then
187 #echo "diff $drblmac/macadr-auto-$eth $drblmac/macadr-auto-$ethx"
188 #read ANS
189 if [ -z "$(diff $drblmac/macadr-auto-$eth $drblmac/macadr-auto-$ethx)" ]; then
190 [ -f $drblmac/macadr-auto-$ethx ] && rm -f $drblmac/macadr-auto-$ethx
191 ethmatch="$eth=$ethx $ethmatch"
192 fi
193 fi
194 done
195
196 fi
197 done
198
199 collect_flist=
200 for eth in $request_eth_port; do
201 if [ ! -f macadr-$eth.txt ]; then
202 touch macadr-$eth.txt
203 fi
204 collect_flist="$collect_flist macadr-$eth.txt"
205 done
206
207 for ethmac in `ls $drblmac/macadr-auto-*`; do
208 eth=${ethmac/$drblmac\/macadr-auto-/}
209 run=1
210 while [ $run -eq 1 ]; do
211 for match in $ethmatch; do
212 if [ "$macadr" = "" ]; then
213 if ! read macadr ; then
214 run=0
215 break
216 fi
217 fi
218 if [ -n "$(echo "$match" | grep -e "^$eth=")" ]; then
219 ethx=${match/$eth=/}
220 # We only use lowercase MAC address, since pxelinux file name uses lowercase.
221 echo "$macadr" | tr '[A-Z]' '[a-z]' >> macadr-$ethx.txt
222 if ! read macadr ; then
223 run=0
224 break
225 fi
226 fi
227 done
228 done < $ethmac
229 done
230
231 #
232 for imac in ./macadr-*.txt; do
233 dos2unix $imac 2> /dev/null
234 done
235
236 # clean the tmp directory
237 [ -d "$drblmac" -a -n "$(echo $drblmac | grep "drbl_nic")" ] && rm -rf $drblmac
238
239 echo "$msg_delimiter_star_line"
240 # message for re-start dhcpd, xinetd
241 if [ "$service_restart_prompt" = "yes" ]; then
242 echo "$msg_dhcpd_tftpd_stopped_need_restart_if_you_need_them"
243 fi
244 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
245 echo "$msg_the_collected_MAC_addr_from [$request_eth_port] $msg_are_saved_in_files_separately:$collect_flist."
246 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL