| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # License: GPL |
| 3 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 4 | # Description: Program to detect DHCP service. |
| 5 | |
| 6 | # Load DRBL setting and functions |
| 7 | # Setting |
| 8 | # Source function library. |
| 9 | [scip-shell system . . `[`. -f /etc/rc.d/init.d/functionsscip-shell filesystem . . `/etc/rc.d/init.d/functions`. ] && . /etc/rc.d/init.d/functionsscip-shell source . . `/etc/rc.d/init.d/functions`. |
| 10 | |
| 11 | # Load DRBL setting and functions |
| 12 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 13 | |
| 14 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 15 | . /etc/drbl/drbl-ocs.confscip-shell source . . `/etc/drbl/drbl-ocs.conf`. |
| 16 | . $DRBL_SCRIPT_PATH/sbin/ocs-functions |
| 17 | |
| 18 | # Settings |
| 19 | timeout_def="30" |
| 20 | |
| 21 | # |
| 22 | USAGEscip-shell shell shell-project 0.0.0 USAGE().() { |
| 23 | echoscip-shell system . . echo. "$ocs - To detect DHCP service" |
| 24 | echoscip-shell system . . echo. "Usage:" |
| 25 | echoscip-shell system . . echo. "To run $ocs:" |
| 26 | echoscip-shell system . . echo. "$ocs [OPTION] OUTPUT_FILE" |
| 27 | echoscip-shell system . . echo. "Options:" |
| 28 | echoscip-shell system . . echo. "-i, --interface DEV Specify the ethernet dev for detecting DHCP service" |
| 29 | echoscip-shell system . . echo. "-t, --timeout TIME Specify the timeout (sec) for detecting DHCP service" |
| 30 | echoscip-shell system . . echo. "OUTPUT_FILE is for saving the info, including: Ethernet card, DHCP server, available DHCP client. If no file is assigned, the results will be shown on stdout only." |
| 31 | echoscip-shell system . . echo. "Ex:" |
| 32 | echoscip-shell system . . echo. "To detect DHCP service on network card eth1, and output the results to file \"dhcp.txt\", run:" |
| 33 | echoscip-shell system . . echo. " $ocs -i eth1 dhcp.txt" |
| 34 | echoscip-shell system . . echo. |
| 35 | } # end of USAGE |
| 36 | # |
| 37 | get_dhcpsrv_info_from_nagiosscip-shell shell shell-project 0.0.0 get_dhcpsrv_info_from_nagios().() { |
| 38 | local eth_ d_result |
| 39 | local dhcp_srv_ip dhcp_client_ip rc_=1 |
| 40 | eth_="$1" |
| 41 | d_result="$2" |
| 42 | if [scip-shell system . . `[`. -z "$eth_" ]; then |
| 43 | echoscip-shell system . . echo. "Variable eth_ was not assigned in function get_dhcpsrv_info_from_nagios." |
| 44 | exit 1 |
| 45 | fi |
| 46 | if [scip-shell system . . `[`. -z "$d_result" ]; then |
| 47 | echoscip-shell system . . echo. "Variable d_result was not assigned in function get_dhcpsrv_info_from_nagios." |
| 48 | exit 1 |
| 49 | fi |
| 50 | if [scip-shell system . . `[`. ! -x /usr/lib/nagios/plugins/check_dhcpscip-shell filesystem . . `/usr/lib/nagios/plugins/check_dhcp`. ]; then |
| 51 | echoscip-shell system . . echo. "File /usr/lib/nagios/plugins/check_dhcp was not found. Skip Nagios method." |
| 52 | return 1 |
| 53 | fi |
| 54 | # TODO: check_dhcp might quit very quickly. How to run it until we find the results, but not keep it too long? |
| 55 | /usr/lib/nagios/plugins/check_dhcpscip-shell filesystem . . `/usr/lib/nagios/plugins/check_dhcp`. -v -i $eth_ > $d_result & |
| 56 | time_left="$timeout" |
| 57 | while [scip-shell system . . `[`. "$time_left" -ne 0 ]; do |
| 58 | echoscip-shell system . . echo. -n "$time_left " |
| 59 | # Result is like: |
| 60 | # recv_result: 300 |
| 61 | # receive_dhcp_packet() result: 300 |
| 62 | # receive_dhcp_packet() source: 192.168.22.254 |
| 63 | # Result=OK |
| 64 | # DHCPOFFER from IP address 192.168.22.254 via 192.168.22.254 |
| 65 | # DHCPOFFER XID: 699683243 (0x29B451AB) |
| 66 | # DHCPOFFER chaddr: 000C2992D4D0 |
| 67 | # DHCPOFFER ciaddr: 0.0.0.0 |
| 68 | # DHCPOFFER yiaddr: 192.168.22.1 |
| 69 | # DHCPOFFER siaddr: 192.168.22.254 |
| 70 | # DHCPOFFER giaddr: 0.0.0.0 |
| 71 | dhcp_srv_ip="$(LC_ALL=C grepscip-shell system . . grep. -E "^DHCPOFFER siaddr:" $d_result 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. -F":" '{print $2}')" |
| 72 | dhcp_client_ip="$(LC_ALL=C grepscip-shell system . . grep. -E "^DHCPOFFER yiaddr:" $d_result 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. -F":" '{print $2}')" |
| 73 | # Strip the leading white spaces |
| 74 | dhcp_srv_ip="$(echoscip-shell system . . echo. $dhcp_srv_ip)" |
| 75 | dhcp_client_ip="$(echoscip-shell system . . echo. $dhcp_client_ip)" |
| 76 | if [scip-shell system . . `[`. -n "$dhcp_srv_ip" -a -n "$dhcp_client_ip" ]; then |
| 77 | dhcp_srv_ip="$(echoscip-shell system . . echo. $dhcp_srv_ip)" # Strip leading spaces. |
| 78 | dhcp_client_ip="$(echoscip-shell system . . echo. $dhcp_client_ip)" # Strip leading spaces. |
| 79 | echoscip-shell system . . echo. |
| 80 | echoscip-shell system . . echo. "Found DHCP service. Ethernet card, DHCP server, Available DHCP client: $eth_, $dhcp_srv_ip, $dhcp_client_ip" |
| 81 | if [scip-shell system . . `[`. -n "$out_f" ]; then |
| 82 | catscip-shell system . . cat. <<-DHCP_END > $out_f |
| 83 | DHCPOFFER_eth="$eth_" |
| 84 | DHCPOFFER_siaddr="$dhcp_srv_ip" |
| 85 | DHCPOFFER_yiaddr="$dhcp_client_ip" |
| 86 | DHCPOFFER_netmask="" |
| 87 | DHCPOFFER_prefix="" |
| 88 | DHCP_END |
| 89 | fi |
| 90 | rc_=0 |
| 91 | break |
| 92 | fi |
| 93 | sleepscip-shell system . . sleep. 1 |
| 94 | time_left="$((time_left - 1))" |
| 95 | done |
| 96 | return $rc_ |
| 97 | } # end of get_dhcpsrv_info_from_nagios |
| 98 | # |
| 99 | get_dhcpsrv_info_from_nmapscip-shell shell shell-project 0.0.0 get_dhcpsrv_info_from_nmap().() { |
| 100 | local eth_ d_result |
| 101 | local dhcp_srv_ip dhcp_client_ip dhcp_client_nm dhcp_client_prefix rc_=1 |
| 102 | eth_="$1" |
| 103 | d_result="$2" |
| 104 | if [scip-shell system . . `[`. -z "$eth_" ]; then |
| 105 | echoscip-shell system . . echo. "Variable eth_ was not assigned in function get_dhcpsrv_info_from_nmap." |
| 106 | exit 1 |
| 107 | fi |
| 108 | if [scip-shell system . . `[`. -z "$d_result" ]; then |
| 109 | echoscip-shell system . . echo. "Variable d_result was not assigned in function get_dhcpsrv_info_from_nmap." |
| 110 | exit 1 |
| 111 | fi |
| 112 | if ! type nmap >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1; then |
| 113 | echoscip-shell system . . echo. "Program nmap was not found. Skip nmap method." |
| 114 | return 1 |
| 115 | fi |
| 116 | LC_ALL=C nmap --script broadcast-dhcp-discover -e $eth_ > $d_result 2>&1 & |
| 117 | time_left="$timeout" |
| 118 | while [scip-shell system . . `[`. "$time_left" -ne 0 ]; do |
| 119 | echoscip-shell system . . echo. -n "$time_left " |
| 120 | # Nmap result is like: |
| 121 | # nmap --script broadcast-dhcp-discover -e macvlan0 |
| 122 | # |
| 123 | # Starting Nmap 7.12 ( https://nmap.org ) at 2017-01-29 18:09 CST |
| 124 | # Pre-scan script results: |
| 125 | # | broadcast-dhcp-discover: |
| 126 | # | Response 1 of 1: |
| 127 | # | IP Offered: 192.168.120.5 |
| 128 | # | DHCP Message Type: DHCPOFFER |
| 129 | # | Server Identifier: 192.168.120.254 |
| 130 | # | IP Address Lease Time: 5m00s |
| 131 | # | Subnet Mask: 255.255.255.0 |
| 132 | # | Router: 192.168.120.254 |
| 133 | # | Domain Name Server: 8.8.8.8, 8.8.4.4 |
| 134 | # | Domain Name: nchc.org.tw |
| 135 | dhcp_srv_ip="$(LC_ALL=C grepscip-shell system . . grep. -E "^\|[[:space:]]*Server Identifier:" $d_result 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. -F":" '{print $2}')" |
| 136 | dhcp_client_ip="$(LC_ALL=C grepscip-shell system . . grep. -E "^\|[[:space:]]*IP Offered:" $d_result 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. -F":" '{print $2}')" |
| 137 | dhcp_client_nm="$(LC_ALL=C grepscip-shell system . . grep. -E "^\|[[:space:]]*Subnet Mask:" $d_result 2>/dev/nullscip-shell filesystem . . `/dev/null`. | awkscip-shell system . . awk. -F":" '{print $2}')" |
| 138 | # Strip the leading white spaces |
| 139 | dhcp_srv_ip="$(echoscip-shell system . . echo. $dhcp_srv_ip)" |
| 140 | dhcp_client_ip="$(echoscip-shell system . . echo. $dhcp_client_ip)" |
| 141 | dhcp_client_nm="$(echoscip-shell system . . echo. $dhcp_client_nm)" |
| 142 | # convert netmask to prefix length |
| 143 | if [scip-shell system . . `[`. -n "$dhcp_client_nm" ]; then |
| 144 | dhcp_client_prefix="$(LC_ALL=C drbl-ipcalc 0.0.0.0 $dhcp_client_nm | awkscip-shell system . . awk. -F" " '/^Netmask:/ {print $4}scip-shell filesystem . . `/^Netmask:/ {print $4}`.')" |
| 145 | fi |
| 146 | if [scip-shell system . . `[`. -n "$dhcp_srv_ip" -a -n "$dhcp_client_ip" ]; then |
| 147 | dhcp_srv_ip="$(echoscip-shell system . . echo. $dhcp_srv_ip)" # Strip leading spaces. |
| 148 | dhcp_client_ip="$(echoscip-shell system . . echo. $dhcp_client_ip)" # Strip leading spaces. |
| 149 | echoscip-shell system . . echo. |
| 150 | echoscip-shell system . . echo. "Found DHCP service. Ethernet card, DHCP server, Available DHCP client: $eth_, $dhcp_srv_ip, $dhcp_client_ip" |
| 151 | if [scip-shell system . . `[`. -n "$out_f" ]; then |
| 152 | catscip-shell system . . cat. <<-DHCP_END > $out_f |
| 153 | DHCPOFFER_eth="$eth_" |
| 154 | DHCPOFFER_siaddr="$dhcp_srv_ip" |
| 155 | DHCPOFFER_yiaddr="$dhcp_client_ip" |
| 156 | DHCPOFFER_netmask="$dhcp_client_nm" |
| 157 | DHCPOFFER_prefix="$dhcp_client_prefix" |
| 158 | DHCP_END |
| 159 | fi |
| 160 | rc_=0 |
| 161 | break |
| 162 | fi |
| 163 | sleepscip-shell system . . sleep. 1 |
| 164 | time_left="$((time_left - 1))" |
| 165 | done |
| 166 | return $rc_ |
| 167 | } # end of get_dhcpsrv_info_from_nmap |
| 168 | # |
| 169 | get_dhcpsrv_info_from_udhcpcscip-shell shell shell-project 0.0.0 get_dhcpsrv_info_from_udhcpc().(){ |
| 170 | local dhcp_srv_ip dhcp_client_ip rc_ |
| 171 | local eth_ |
| 172 | eth_="$1" |
| 173 | rc_="1" |
| 174 | # Check if busybox supports udhcpc |
| 175 | if [scip-shell system . . `[`. -z "$(LC_ALL=C busybox 2>/dev/nullscip-shell filesystem . . `/dev/null`. | grepscip-shell system . . grep. -Ewo "udhcpc")" ]; then |
| 176 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 177 | echoscip-shell system . . echo. "Busybox does not porvide applet udhcpc!" |
| 178 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 179 | return 1 |
| 180 | fi |
| 181 | # Generate the script for udhcpc to run |
| 182 | # Try 5 times |
| 183 | # The output file for udhcpc-find-dhcp-record: udhdpc_query_result is defined in drbl.conf |
| 184 | ocs_log_rotate $udhdpc_query_result |
| 185 | busybox udhcpc -t 5 -n -q -s $DRBL_SCRIPT_PATH/bin/udhcpc-find-dhcp-record -i $eth_ >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 & |
| 186 | time_left="$timeout" |
| 187 | while [scip-shell system . . `[`. "$time_left" -ne 0 ]; do |
| 188 | echoscip-shell system . . echo. -n "$time_left " |
| 189 | dhcp_srv_ip="" |
| 190 | dhcp_client_ip="" |
| 191 | [scip-shell system . . `[`. -e "$udhdpc_query_result" ] && . $udhdpc_query_result |
| 192 | dhcp_srv_ip="$DHCPOFFER_siaddr" |
| 193 | dhcp_client_ip="$DHCPOFFER_yiaddr" |
| 194 | if [scip-shell system . . `[`. -n "$dhcp_srv_ip" -a -n "$dhcp_client_ip" ]; then |
| 195 | echoscip-shell system . . echo. |
| 196 | echoscip-shell system . . echo. "Found DHCP service. Ethernet card, DHCP server, Available DHCP client: $eth_, $dhcp_srv_ip, $dhcp_client_ip" |
| 197 | rc_=0 |
| 198 | # Save the output file before exiting |
| 199 | cpscip-shell system . . cp. -a $udhdpc_query_result $out_f |
| 200 | break |
| 201 | fi |
| 202 | sleepscip-shell system . . sleep. 1 |
| 203 | time_left="$((time_left - 1))" |
| 204 | done |
| 205 | return $rc_ |
| 206 | } # end of get_dhcpsrv_info_from_udhcpc |
| 207 | |
| 208 | #################### |
| 209 | ### Main program ### |
| 210 | #################### |
| 211 | |
| 212 | ocs_file="$0" |
| 213 | ocs=`basenamescip-shell system . . basename. $ocs_file` |
| 214 | # |
| 215 | while [scip-shell system . . `[`. $# -gt 0 ]; do |
| 216 | case "$1" in |
| 217 | -i|--interface) |
| 218 | shift; |
| 219 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 220 | # skip the -xx option, in case |
| 221 | eth_port="$1" |
| 222 | shift; |
| 223 | fi |
| 224 | [scip-shell system . . `[`. -z "$eth_port" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 225 | ;; |
| 226 | -t|--timeout) |
| 227 | shift; |
| 228 | if [scip-shell system . . `[`. -z "$(echoscip-shell system . . echo. $1 |grepscip-shell system . . grep. ^-.)" ]; then |
| 229 | # skip the -xx option, in case |
| 230 | timeout="$1" |
| 231 | shift; |
| 232 | fi |
| 233 | [scip-shell system . . `[`. -z "$timeout" ] && USAGEscip-shell shell shell-project 0.0.0 USAGE(). && exit 1 |
| 234 | ;; |
| 235 | -*) echoscip-shell system . . echo. "${0}: ${1}: invalid option" >&2 |
| 236 | USAGEscip-shell shell shell-project 0.0.0 USAGE(). >& 2 |
| 237 | exit 2 ;; |
| 238 | *) break ;; |
| 239 | esac |
| 240 | done |
| 241 | out_f="$1" |
| 242 | |
| 243 | # |
| 244 | [scip-shell system . . `[`. -z "$timeout" ] && timeout="$timeout_def" |
| 245 | # |
| 246 | ask_and_load_lang_set |
| 247 | |
| 248 | # |
| 249 | if [scip-shell system . . `[`. ! -e /usr/lib/nagios/plugins/check_dhcpscip-shell filesystem . . `/usr/lib/nagios/plugins/check_dhcp`. ] && ! `type nmap >/dev/nullscip-shell filesystem . . `/dev/null`. 2>&1` ; then |
| 250 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 251 | echoscip-shell system . . echo. "File /usr/lib/nagios/plugins/check_dhcp or program nmap not found!" |
| 252 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 253 | echoscip-shell system . . echo. "Please install Nagios plugins or nmap program. On Debian/Ubuntu system, the required package is monitoring-plugins-basic or nmap." |
| 254 | echoscip-shell system . . echo. "$msg_program_stop!" |
| 255 | my_ocs_exit 1 |
| 256 | fi |
| 257 | # |
| 258 | if [scip-shell system . . `[`. -z "$eth_port" ]; then |
| 259 | # Detect on all ethernet ports |
| 260 | eth_port="$(LC_ALL=C get-nic-devs)" |
| 261 | else |
| 262 | # Check if device exists |
| 263 | for i in $eth_port; do |
| 264 | if [scip-shell system . . `[`. ! -e /sys/class/net/$i ]; then |
| 265 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 266 | echoscip-shell system . . echo. "Interface $i not found!" |
| 267 | [scip-shell system . . `[`. "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 268 | echoscip-shell system . . echo. "$msg_program_stop!" |
| 269 | my_ocs_exit 1 |
| 270 | fi |
| 271 | done |
| 272 | fi |
| 273 | |
| 274 | dhcp_req_result="$(mktempscip-shell system . . mktemp. /tmp/DHCP_REQ.XXXXXXscip-shell filesystem . . `/tmp/DHCP_REQ.XXXXXX`.)" |
| 275 | rc=1 |
| 276 | for ieth in $eth_port; do |
| 277 | ip link set $ieth up |
| 278 | # Clean content first. |
| 279 | > $dhcp_req_result |
| 280 | echoscip-shell system . . echo. -n "Detecting DHCP services... " |
| 281 | get_dhcpsrv_info_from_udhcpcscip-shell shell shell-project 0.0.0 get_dhcpsrv_info_from_udhcpc(). $ieth $dhcp_req_result |
| 282 | rc=$? |
| 283 | if [scip-shell system . . `[`. "$rc" -ne 0 ]; then |
| 284 | echoscip-shell system . . echo. "" |
| 285 | echoscip-shell system . . echo. "Trying another method, i.e., Nmap way..." |
| 286 | get_dhcpsrv_info_from_nmapscip-shell shell shell-project 0.0.0 get_dhcpsrv_info_from_nmap(). $ieth $dhcp_req_result |
| 287 | rc=$? |
| 288 | if [scip-shell system . . `[`. "$rc" -ne 0 ]; then |
| 289 | echoscip-shell system . . echo. "" |
| 290 | echoscip-shell system . . echo. "Trying another method, i.e., Nagios way..." |
| 291 | get_dhcpsrv_info_from_nagiosscip-shell shell shell-project 0.0.0 get_dhcpsrv_info_from_nagios(). $ieth $dhcp_req_result |
| 292 | rc=$? |
| 293 | fi |
| 294 | fi |
| 295 | done |
| 296 | |
| 297 | # Clean temp file |
| 298 | rmscip-shell system . . rm. -f $dhcp_req_result |
| 299 | # |
| 300 | exit $rc |