Debian Codegraph @

sbin/drbl-find-dhcp-srv

raw symbols SCIP
1 #!/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 [ -f /etc/rc.d/init.d/functions ] && . /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.conf
16 . $DRBL_SCRIPT_PATH/sbin/ocs-functions
17
18 # Settings
19 timeout_def="30"
20
21 #
22 USAGE() {
23 echo "$ocs - To detect DHCP service"
24 echo "Usage:"
25 echo "To run $ocs:"
26 echo "$ocs [OPTION] OUTPUT_FILE"
27 echo "Options:"
28 echo "-i, --interface DEV Specify the ethernet dev for detecting DHCP service"
29 echo "-t, --timeout TIME Specify the timeout (sec) for detecting DHCP service"
30 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 echo "Ex:"
32 echo "To detect DHCP service on network card eth1, and output the results to file \"dhcp.txt\", run:"
33 echo " $ocs -i eth1 dhcp.txt"
34 echo
35 } # end of USAGE
36 #
37 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 [ -z "$eth_" ]; then
43 echo "Variable eth_ was not assigned in function get_dhcpsrv_info_from_nagios."
44 exit 1
45 fi
46 if [ -z "$d_result" ]; then
47 echo "Variable d_result was not assigned in function get_dhcpsrv_info_from_nagios."
48 exit 1
49 fi
50 if [ ! -x /usr/lib/nagios/plugins/check_dhcp ]; then
51 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_dhcp -v -i $eth_ > $d_result &
56 time_left="$timeout"
57 while [ "$time_left" -ne 0 ]; do
58 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 grep -E "^DHCPOFFER siaddr:" $d_result 2>/dev/null | awk -F":" '{print $2}')"
72 dhcp_client_ip="$(LC_ALL=C grep -E "^DHCPOFFER yiaddr:" $d_result 2>/dev/null | awk -F":" '{print $2}')"
73 # Strip the leading white spaces
74 dhcp_srv_ip="$(echo $dhcp_srv_ip)"
75 dhcp_client_ip="$(echo $dhcp_client_ip)"
76 if [ -n "$dhcp_srv_ip" -a -n "$dhcp_client_ip" ]; then
77 dhcp_srv_ip="$(echo $dhcp_srv_ip)" # Strip leading spaces.
78 dhcp_client_ip="$(echo $dhcp_client_ip)" # Strip leading spaces.
79 echo
80 echo "Found DHCP service. Ethernet card, DHCP server, Available DHCP client: $eth_, $dhcp_srv_ip, $dhcp_client_ip"
81 if [ -n "$out_f" ]; then
82 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 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_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 [ -z "$eth_" ]; then
105 echo "Variable eth_ was not assigned in function get_dhcpsrv_info_from_nmap."
106 exit 1
107 fi
108 if [ -z "$d_result" ]; then
109 echo "Variable d_result was not assigned in function get_dhcpsrv_info_from_nmap."
110 exit 1
111 fi
112 if ! type nmap >/dev/null 2>&1; then
113 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 [ "$time_left" -ne 0 ]; do
119 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 grep -E "^\|[[:space:]]*Server Identifier:" $d_result 2>/dev/null | awk -F":" '{print $2}')"
136 dhcp_client_ip="$(LC_ALL=C grep -E "^\|[[:space:]]*IP Offered:" $d_result 2>/dev/null | awk -F":" '{print $2}')"
137 dhcp_client_nm="$(LC_ALL=C grep -E "^\|[[:space:]]*Subnet Mask:" $d_result 2>/dev/null | awk -F":" '{print $2}')"
138 # Strip the leading white spaces
139 dhcp_srv_ip="$(echo $dhcp_srv_ip)"
140 dhcp_client_ip="$(echo $dhcp_client_ip)"
141 dhcp_client_nm="$(echo $dhcp_client_nm)"
142 # convert netmask to prefix length
143 if [ -n "$dhcp_client_nm" ]; then
144 dhcp_client_prefix="$(LC_ALL=C drbl-ipcalc 0.0.0.0 $dhcp_client_nm | awk -F" " '/^Netmask:/ {print $4}')"
145 fi
146 if [ -n "$dhcp_srv_ip" -a -n "$dhcp_client_ip" ]; then
147 dhcp_srv_ip="$(echo $dhcp_srv_ip)" # Strip leading spaces.
148 dhcp_client_ip="$(echo $dhcp_client_ip)" # Strip leading spaces.
149 echo
150 echo "Found DHCP service. Ethernet card, DHCP server, Available DHCP client: $eth_, $dhcp_srv_ip, $dhcp_client_ip"
151 if [ -n "$out_f" ]; then
152 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 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_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 [ -z "$(LC_ALL=C busybox 2>/dev/null | grep -Ewo "udhcpc")" ]; then
176 [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
177 echo "Busybox does not porvide applet udhcpc!"
178 [ "$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/null 2>&1 &
186 time_left="$timeout"
187 while [ "$time_left" -ne 0 ]; do
188 echo -n "$time_left "
189 dhcp_srv_ip=""
190 dhcp_client_ip=""
191 [ -e "$udhdpc_query_result" ] && . $udhdpc_query_result
192 dhcp_srv_ip="$DHCPOFFER_siaddr"
193 dhcp_client_ip="$DHCPOFFER_yiaddr"
194 if [ -n "$dhcp_srv_ip" -a -n "$dhcp_client_ip" ]; then
195 echo
196 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 cp -a $udhdpc_query_result $out_f
200 break
201 fi
202 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=`basename $ocs_file`
214 #
215 while [ $# -gt 0 ]; do
216 case "$1" in
217 -i|--interface)
218 shift;
219 if [ -z "$(echo $1 |grep ^-.)" ]; then
220 # skip the -xx option, in case
221 eth_port="$1"
222 shift;
223 fi
224 [ -z "$eth_port" ] && USAGE && exit 1
225 ;;
226 -t|--timeout)
227 shift;
228 if [ -z "$(echo $1 |grep ^-.)" ]; then
229 # skip the -xx option, in case
230 timeout="$1"
231 shift;
232 fi
233 [ -z "$timeout" ] && USAGE && exit 1
234 ;;
235 -*) echo "${0}: ${1}: invalid option" >&2
236 USAGE >& 2
237 exit 2 ;;
238 *) break ;;
239 esac
240 done
241 out_f="$1"
242
243 #
244 [ -z "$timeout" ] && timeout="$timeout_def"
245 #
246 ask_and_load_lang_set
247
248 #
249 if [ ! -e /usr/lib/nagios/plugins/check_dhcp ] && ! `type nmap >/dev/null 2>&1` ; then
250 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
251 echo "File /usr/lib/nagios/plugins/check_dhcp or program nmap not found!"
252 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
253 echo "Please install Nagios plugins or nmap program. On Debian/Ubuntu system, the required package is monitoring-plugins-basic or nmap."
254 echo "$msg_program_stop!"
255 my_ocs_exit 1
256 fi
257 #
258 if [ -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 [ ! -e /sys/class/net/$i ]; then
265 [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
266 echo "Interface $i not found!"
267 [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
268 echo "$msg_program_stop!"
269 my_ocs_exit 1
270 fi
271 done
272 fi
273
274 dhcp_req_result="$(mktemp /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 echo -n "Detecting DHCP services... "
281 get_dhcpsrv_info_from_udhcpc $ieth $dhcp_req_result
282 rc=$?
283 if [ "$rc" -ne 0 ]; then
284 echo ""
285 echo "Trying another method, i.e., Nmap way..."
286 get_dhcpsrv_info_from_nmap $ieth $dhcp_req_result
287 rc=$?
288 if [ "$rc" -ne 0 ]; then
289 echo ""
290 echo "Trying another method, i.e., Nagios way..."
291 get_dhcpsrv_info_from_nagios $ieth $dhcp_req_result
292 rc=$?
293 fi
294 fi
295 done
296
297 # Clean temp file
298 rm -f $dhcp_req_result
299 #
300 exit $rc