| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # Steven Shiau <[email protected]> |
| 3 | # License: GPL |
| 4 | # Description: To get the IP address lists from dhcpd.conf |
| 5 | |
| 6 | # Loading setting. |
| 7 | # Load DRBL setting and functions |
| 8 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 9 | |
| 10 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 11 | |
| 12 | # Settings |
| 13 | sort_output="yes" |
| 14 | dhcpd_conf_def="$DHCPDCONF_DIR/dhcpd.conf" |
| 15 | |
| 16 | # |
| 17 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 18 | echoscip-shell system . . echo. "$ocs - To get the DRBL clients' IP addresses list from dhcpd.conf" |
| 19 | echoscip-shell system . . echo. "Usage:" |
| 20 | echoscip-shell system . . echo. "To run $ocs:" |
| 21 | echoscip-shell system . . echo. "$ocs [OPTION]" |
| 22 | echoscip-shell system . . echo. "Options:" |
| 23 | echoscip-shell system . . echo. "-w, --without-sorting List the clients' IP addresses list without sorting" |
| 24 | echoscip-shell system . . echo. "-c, --dhcpd-config FILE Assign the DHCPD config file. By default $dhcpd_conf_def is used" |
| 25 | echoscip-shell system . . echo. "-v, --verbose Run in verbose mode" |
| 26 | echoscip-shell system . . echo. |
| 27 | } # end of USAGE |
| 28 | |
| 29 | #################### |
| 30 | ### Main program ### |
| 31 | #################### |
| 32 | |
| 33 | ocs_file="$0" |
| 34 | ocs=`basenamescip-shell system . . basename. $ocs_file` |
| 35 | # Parse command-line options |
| 36 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 37 | case "$1" in |
| 38 | -w|--without-sorting) |
| 39 | shift; sort_output="no" |
| 40 | ;; |
| 41 | -c|--dhcpd-config) |
| 42 | shift; |
| 43 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 44 | # skip the -xx option, in case |
| 45 | dhcpd_conf="$1" |
| 46 | shift; |
| 47 | fi |
| 48 | [scip-shell system . . `[`. -z "$dhcpd_conf" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 49 | ;; |
| 50 | -v|--verbose) |
| 51 | shift; VERBOSE="on" |
| 52 | ;; |
| 53 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 54 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2 |
| 55 | exit 2 ;; |
| 56 | *) break ;; |
| 57 | esac |
| 58 | done |
| 59 | |
| 60 | # |
| 61 | if [scip-shell system . . `[`. -z "$dhcpd_conf" ]; then |
| 62 | dhcpd_conf="$dhcpd_conf_def" |
| 63 | fi |
| 64 | if [scip-shell system . . `[`. ! -e "$dhcpd_conf" ]; then |
| 65 | exit 1 |
| 66 | fi |
| 67 | # |
| 68 | IPLIST="$(mktempscip-shell system . . mktemp. /tmp/iplist.XXXXXXscip-shell filesystem . . `/tmp/iplist.XXXXXX`.)" |
| 69 | hn_ip_mac="$(mktempscip-shell system . . mktemp. /tmp/hn_ip_mac.XXXXXXscip-shell filesystem . . `/tmp/hn_ip_mac.XXXXXX`.)" |
| 70 | ip_range_nm="$(mktempscip-shell system . . mktemp. /tmp/ip_range_nm.XXXXXXscip-shell filesystem . . `/tmp/ip_range_nm.XXXXXX`.)" |
| 71 | |
| 72 | # (1) for MAC address part |
| 73 | parse_dhcpd_conf $hn_ip_mac >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 74 | LC_ALL=C grepscip-shell system . . grep. -E "^[[:space:]]*[^#]" $hn_ip_mac | awkscip-shell system . . awk. -F" " '{print $2}' | catscip-shell system . . cat. > $IPLIST |
| 75 | |
| 76 | # (2) For range part |
| 77 | LC_ALL=C grepscip-shell system . . grep. -iEo "^(^subnet[[:space:]]+[([:digit:]|\.]+[[:space:]]+netmask.*\{|[[:space:]]*range.*;)" $dhcpd_conf > $ip_range_nm |
| 78 | |
| 79 | # The results in $ip_range_nm might like this (range in the subnet 192.168.27.0, and MAC addresses used in the subnet 172.16.0.0): |
| 80 | # subnet 192.168.27.0 netmask 255.255.255.0 { |
| 81 | # range 192.168.27.1 192.168.27.206; |
| 82 | # subnet 172.16.0.0 netmask 255.255.0.0 { |
| 83 | |
| 84 | range_lines="$(LC_ALL=C grepscip-shell system . . grep. -n -iEo "^[[:space:]]*range.*;" $ip_range_nm | awkscip-shell system . . awk. -F":" '{print $1}')" |
| 85 | for i in $range_lines; do |
| 86 | pl="$((i-1))" |
| 87 | netmask="$(LC_ALL=C perlscip-shell system . . perl. -ne "$pl..$i and print" $ip_range_nm | grepscip-shell system . . grep. -Eo "netmask[[:space:]]+[([:digit:]|\.]+" | awkscip-shell system . . awk. -F" " '{print $2}')" |
| 88 | range="$(LC_ALL=C perlscip-shell system . . perl. -ne "$pl..$i and print" $ip_range_nm | grepscip-shell system . . grep. -Eo "^[[:space:]]*range.*;" | sedscip-shell system . . sed. -r -e "s/range //g" -e "s/;//g" -e "s/^[[:space:]]*//g")" |
| 89 | drbl-ipcalc-list -r $range $netmask >> $IPLIST |
| 90 | done |
| 91 | # We need to remove the DRBL server's IP address if they are in $IPLIST. In the dhcpd.conf, the range option contains a range, but any IP address of DRBL server might contain that, and we should not show that as the IP addresses of DRBL clients. |
| 92 | srv_IPs="$(LC_ALL=C get-all-nic-ip -b)" |
| 93 | for i in $srv_IPs; do |
| 94 | LC_ALL=C perlscip-shell system . . perl. -pi -e "s/\b$i\b\n//g" $IPLIST |
| 95 | done |
| 96 | |
| 97 | # |
| 98 | case "$sort_output" in |
| 99 | yes) LC_ALL=C catscip-shell system . . cat. $IPLIST | sortscip-shell system . . sort. -V | uniqscip-shell system . . uniq. ;; |
| 100 | no) LC_ALL=C catscip-shell system . . cat. $IPLIST ;; |
| 101 | esac |
| 102 | |
| 103 | # clean |
| 104 | [scip-shell system . . `[`. -f "$IPLIST" ] && rmscip-shell system . . rm. -f $IPLIST |
| 105 | [scip-shell system . . `[`. -f "$hn_ip_mac" ] && rmscip-shell system . . rm. -f $hn_ip_mac |
| 106 | [scip-shell system . . `[`. -f "$ip_range_nm" ] && rmscip-shell system . . rm. -f $ip_range_nm |
| 107 | |
| 108 | exit 0 |