| 1 | #!/bin/bashscip-shell filesystem . . `/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 | usagescip-shell shell shell-project 0.0.0 usage().() { |
| 20 | echoscip-shell system . . echo. "Collect the MAC addresses of DRBL." |
| 21 | echoscip-shell system . . echo. "Usage: $0 [OPTION] request_ethernet_port_list..." |
| 22 | echoscip-shell system . . echo. "OPTION:" |
| 23 | language_help_prompt_by_idx_no |
| 24 | echoscip-shell system . . echo. "-v, --verbose: verbose mode." |
| 25 | echoscip-shell system . . echo. "-n, --no-service-prompt: do not show service restart messages." |
| 26 | echoscip-shell system . . 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 [scip-shell system . . `[`. $# -gt 0 ]; do |
| 42 | case "$1" in |
| 43 | -l|--language) |
| 44 | shift |
| 45 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . 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 | echoscip-shell system . . echo. "request_eth_port:$request_eth_port" |
| 68 | if [scip-shell system . . `[`. -z "$request_eth_port" ]; then |
| 69 | usagescip-shell shell shell-project 0.0.0 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 [scip-shell system . . `[`. -e /etc/debian_versionscip-shell filesystem . . `/etc/debian_version`. ]; then |
| 79 | # Debian |
| 80 | for serv_st in $server_to_stop_now_debian; do |
| 81 | echoscip-shell system . . echo. "Stopping $serv_st ..." |
| 82 | RETVAL=0 |
| 83 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$serv_st`.serv_st stop |
| 84 | RETVAL=$? |
| 85 | if [scip-shell system . . `[`. "$RETVAL" -gt 0 ]; then |
| 86 | echoscip-shell system . . echo. "XXXXXXX XXXXXXX XXXXXXX" |
| 87 | echoscip-shell system . . echo. "Failed to stop service $serv_st !!!" |
| 88 | exit $RETVAL |
| 89 | fi |
| 90 | done |
| 91 | elif [scip-shell system . . `[`. -e /etc/SuSE-releasescip-shell filesystem . . `/etc/SuSE-release`. ]; then |
| 92 | # SuSE |
| 93 | for serv_st in $server_to_stop_now_suse; do |
| 94 | # service is running |
| 95 | echoscip-shell system . . echo. "Stopping $serv_st ..." |
| 96 | RETVAL=0 |
| 97 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$serv_st`.serv_st stop |
| 98 | RETVAL=$? |
| 99 | if [scip-shell system . . `[`. "$RETVAL" -gt 0 ]; then |
| 100 | echoscip-shell system . . echo. "XXXXXXX XXXXXXX XXXXXXX" |
| 101 | echoscip-shell system . . 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 [scip-shell system . . `[`. -f /var/lock/subsys/$serv_st ] ; then |
| 109 | # service is running |
| 110 | echoscip-shell system . . echo. "Stopping $serv_st ..." |
| 111 | RETVAL=0 |
| 112 | /etc/init.d/$scip-shell filesystem . . `/etc/init.d/$serv_st`.serv_st stop |
| 113 | RETVAL=$? |
| 114 | if [scip-shell system . . `[`. "$RETVAL" -gt 0 ]; then |
| 115 | echoscip-shell system . . echo. "XXXXXXX XXXXXXX XXXXXXX" |
| 116 | echoscip-shell system . . 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 [scip-shell system . . `[`. "$(lsscip-shell system . . ls. macadr-eth*.txt 2> /dev/nullscip-shell filesystem . . `/dev/null`.)" != "" ]; then |
| 125 | echoscip-shell system . . echo. "deleting old macadr-eth*.txt file..." |
| 126 | rmscip-shell system . . rm. -f macadr-eth*.txt |
| 127 | fi |
| 128 | |
| 129 | drblmac=$(mktempscip-shell system . . mktemp. -d /tmp/drbl_nic.XXXXXXscip-shell filesystem . . `/tmp/drbl_nic.XXXXXX`.) || exit 1 |
| 130 | ## function: mac_collect_status |
| 131 | mac_collect_statusscip-shell shell shell-project 0.0.0 mac_collect_status().() { |
| 132 | [scip-shell system . . `[`. -f $drblmac/setup-drbl-auto.all ] && rmscip-shell system . . rm. -f $drblmac/setup-drbl-auto.all |
| 133 | for eth in $request_eth_port; do |
| 134 | catscip-shell system . . cat. $drblmac/setup-drbl-auto.$eth >> $drblmac/setup-drbl-auto.all |
| 135 | done |
| 136 | num=$(perlscip-shell system . . perl. -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.all | sortscip-shell system . . sort. | uniqscip-shell system . . uniq. | wcscip-shell system . . wc. | awkscip-shell system . . awk. '{print $1}') |
| 137 | echoscip-shell system . . echo. "=======================================" |
| 138 | #perl -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.all | sort | uniq |
| 139 | # uniq without sort |
| 140 | perlscip-shell system . . perl. -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.all | perlscip-shell system . . perl. -ne 'print unless $lines{$_}++' |
| 141 | echoscip-shell system . . echo. "Total: $num" |
| 142 | echoscip-shell system . . echo. "=======================================" |
| 143 | } |
| 144 | |
| 145 | ## function: stop |
| 146 | stopscip-shell shell shell-project 0.0.0 stop().() { |
| 147 | killall /usr/sbin/tcpdumpscip-shell filesystem . . `/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/tcpdumpscip-shell filesystem . . `/usr/sbin/tcpdump`. -teln -i $eth broadcast and port bootpc > $drblmac/setup-drbl-auto.$eth 2> /dev/nullscip-shell filesystem . . `/dev/null`. & |
| 154 | done |
| 155 | |
| 156 | ## |
| 157 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 158 | echoscip-shell system . . echo. "$msg_start_detect_MAC_addresse" |
| 159 | echoscip-shell system . . echo. "$msg_enter_1_or_press_enter_to_view" |
| 160 | echoscip-shell system . . 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_statusscip-shell shell shell-project 0.0.0 mac_collect_status(). |
| 168 | echoscip-shell system . . echo. "$msg_enter_1_or_press_enter_to_view" |
| 169 | echoscip-shell system . . 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 | perlscip-shell system . . perl. -ane 'print "\U$F[0]\n"' $drblmac/setup-drbl-auto.$eth | perlscip-shell system . . perl. -ne 'print unless $lines{$_}++' > $drblmac/macadr-auto-$eth |
| 178 | done |
| 179 | |
| 180 | ethmatch="" |
| 181 | for eth in $request_eth_port; do |
| 182 | if [scip-shell system . . `[`. -f $drblmac/macadr-auto-$eth ]; then |
| 183 | for ethx in $request_eth_port; do |
| 184 | if [scip-shell system . . `[`. "$ethx" = "$eth" ]; then |
| 185 | ethmatch="$eth=$eth $ethmatch" |
| 186 | elif [scip-shell system . . `[`. -f $drblmac/macadr-auto-$ethx ]; then |
| 187 | #echo "diff $drblmac/macadr-auto-$eth $drblmac/macadr-auto-$ethx" |
| 188 | #read ANS |
| 189 | if [scip-shell system . . `[`. -z "$(diffscip-shell system . . diff. $drblmac/macadr-auto-$eth $drblmac/macadr-auto-$ethx)" ]; then |
| 190 | [scip-shell system . . `[`. -f $drblmac/macadr-auto-$ethx ] && rmscip-shell system . . 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 [scip-shell system . . `[`. ! -f macadr-$eth.txt ]; then |
| 202 | touchscip-shell system . . touch. macadr-$eth.txt |
| 203 | fi |
| 204 | collect_flist="$collect_flist macadr-$eth.txt" |
| 205 | done |
| 206 | |
| 207 | for ethmac in `lsscip-shell system . . ls. $drblmac/macadr-auto-*`; do |
| 208 | eth=${ethmac/$drblmac\/macadr-auto-/} |
| 209 | run=1 |
| 210 | while [scip-shell system . . `[`. $run -eq 1 ]; do |
| 211 | for match in $ethmatch; do |
| 212 | if [scip-shell system . . `[`. "$macadr" = "" ]; then |
| 213 | if ! read macadr ; then |
| 214 | run=0 |
| 215 | break |
| 216 | fi |
| 217 | fi |
| 218 | if [scip-shell system . . `[`. -n "$(echoscip-shell system . . echo. "$match" | grepscip-shell system . . grep. -e "^$eth=")" ]; then |
| 219 | ethx=${match/$eth=/} |
| 220 | # We only use lowercase MAC address, since pxelinux file name uses lowercase. |
| 221 | echoscip-shell system . . echo. "$macadr" | trscip-shell system . . 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/nullscip-shell filesystem . . `/dev/null`. |
| 234 | done |
| 235 | |
| 236 | # clean the tmp directory |
| 237 | [scip-shell system . . `[`. -d "$drblmac" -a -n "$(echoscip-shell system . . echo. $drblmac | grepscip-shell system . . grep. "drbl_nic")" ] && rmscip-shell system . . rm. -rf $drblmac |
| 238 | |
| 239 | echoscip-shell system . . echo. "$msg_delimiter_star_line" |
| 240 | # message for re-start dhcpd, xinetd |
| 241 | if [scip-shell system . . `[`. "$service_restart_prompt" = "yes" ]; then |
| 242 | echoscip-shell system . . echo. "$msg_dhcpd_tftpd_stopped_need_restart_if_you_need_them" |
| 243 | fi |
| 244 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 245 | echoscip-shell system . . echo. "$msg_the_collected_MAC_addr_from [$request_eth_port] $msg_are_saved_in_files_separately:$collect_flist." |
| 246 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |