| 1 | #!/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 | USAGE() { |
| 18 | echo "$ocs - To get the DRBL clients' IP addresses list from dhcpd.conf" |
| 19 | echo "Usage:" |
| 20 | echo "To run $ocs:" |
| 21 | echo "$ocs [OPTION]" |
| 22 | echo "Options:" |
| 23 | echo "-w, --without-sorting List the clients' IP addresses list without sorting" |
| 24 | echo "-c, --dhcpd-config FILE Assign the DHCPD config file. By default $dhcpd_conf_def is used" |
| 25 | echo "-v, --verbose Run in verbose mode" |
| 26 | echo |
| 27 | } # end of USAGE |
| 28 | |
| 29 | #################### |
| 30 | ### Main program ### |
| 31 | #################### |
| 32 | |
| 33 | ocs_file="$0" |
| 34 | ocs=`basename $ocs_file` |
| 35 | # Parse command-line options |
| 36 | while [ $# -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 [ -z "$(echo $1 |grep ^-.)" ]; then |
| 44 | # skip the -xx option, in case |
| 45 | dhcpd_conf="$1" |
| 46 | shift; |
| 47 | fi |
| 48 | [ -z "$dhcpd_conf" ] && USAGE && exit 1 |
| 49 | ;; |
| 50 | -v|--verbose) |
| 51 | shift; VERBOSE="on" |
| 52 | ;; |
| 53 | -*) echo "${0}: ${1}: invalid option" >&2 |
| 54 | USAGE >& 2 |
| 55 | exit 2 ;; |
| 56 | *) break ;; |
| 57 | esac |
| 58 | done |
| 59 | |
| 60 | # |
| 61 | if [ -z "$dhcpd_conf" ]; then |
| 62 | dhcpd_conf="$dhcpd_conf_def" |
| 63 | fi |
| 64 | if [ ! -e "$dhcpd_conf" ]; then |
| 65 | exit 1 |
| 66 | fi |
| 67 | # |
| 68 | IPLIST="$(mktemp /tmp/iplist.XXXXXX)" |
| 69 | hn_ip_mac="$(mktemp /tmp/hn_ip_mac.XXXXXX)" |
| 70 | ip_range_nm="$(mktemp /tmp/ip_range_nm.XXXXXX)" |
| 71 | |
| 72 | # (1) for MAC address part |
| 73 | parse_dhcpd_conf $hn_ip_mac >/dev/null 2>&1 |
| 74 | LC_ALL=C grep -E "^[[:space:]]*[^#]" $hn_ip_mac | awk -F" " '{print $2}' | cat > $IPLIST |
| 75 | |
| 76 | # (2) For range part |
| 77 | LC_ALL=C 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 grep -n -iEo "^[[:space:]]*range.*;" $ip_range_nm | awk -F":" '{print $1}')" |
| 85 | for i in $range_lines; do |
| 86 | pl="$((i-1))" |
| 87 | netmask="$(LC_ALL=C perl -ne "$pl..$i and print" $ip_range_nm | grep -Eo "netmask[[:space:]]+[([:digit:]|\.]+" | awk -F" " '{print $2}')" |
| 88 | range="$(LC_ALL=C perl -ne "$pl..$i and print" $ip_range_nm | grep -Eo "^[[:space:]]*range.*;" | 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 perl -pi -e "s/\b$i\b\n//g" $IPLIST |
| 95 | done |
| 96 | |
| 97 | # |
| 98 | case "$sort_output" in |
| 99 | yes) LC_ALL=C cat $IPLIST | sort -V | uniq ;; |
| 100 | no) LC_ALL=C cat $IPLIST ;; |
| 101 | esac |
| 102 | |
| 103 | # clean |
| 104 | [ -f "$IPLIST" ] && rm -f $IPLIST |
| 105 | [ -f "$hn_ip_mac" ] && rm -f $hn_ip_mac |
| 106 | [ -f "$ip_range_nm" ] && rm -f $ip_range_nm |
| 107 | |
| 108 | exit 0 |