summaryrefslogtreecommitdiffstats
path: root/etc/rc.parse-isc-dhcpd
blob: 4770f8702c7cd492061505671db7a42f237b4555 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/sh

# This script will monitor dhcpd.leases and parse 
# out active leases and ensure they are present 
# in /var/etc/hosts

update_hosts_file() {
	# $1 = host
	# $2 = ip
	cat /var/etc/hosts | grep -v "$1" > /tmp/hosts.tmp
	if [ "$3" != "" ]; then
		echo "$2	$1		# dynamic entry created by rc.parse-isc-dhcpd" >> /tmp/hosts.tmp
	fi
	mv /tmp/hosts.tmp /var/etc/hosts
	killall -HUP dnsmasq
}

# Parse file on initial run
cat /var/dhcpd/var/db/dhcpd.leases | grep "lease" -A8 | while read LINE 
do
	HOSTNAMEA=`echo "$LINE" | grep client-hostname | awk '{ print $2 }' | cut -d'"' -f2`
	ACTIVEA=`echo "$LINE" | grep active`
	IPADDRA=`echo "$LINE" | grep lease | awk '{ print $2 }'`
	if [ "$HOSTNAMEA" != "" ]; then
		HOSTNAME="$HOSTNAMEA"
	fi
	if [ "$ACTIVEA" != "" ]; then
		ACTIVE="$ACTIVEA"
	fi
	if [ "$IPADDRA" != "" ]; then
		IPADDR="$IPADDRA"
	fi
	if [ "$HOSTNAME" != "" ]; then
		if [ "$IPADDR" != "" ]; then
			update_hosts_file "$HOSTNAME" "$IPADDR" "$ACTIVE"
			HOSTNAME=""
			ACTIVE=""
			IPADDR=""
		fi
	fi
done

# After processed go ahead and tail file looking for changes.
tail -F /var/dhcpd/var/db/dhcpd.leases | grep "lease" -A8 | while read LINE 
do
	HOSTNAMEA=`echo "$LINE" | grep client-hostname | awk '{ print $2 }' | cut -d'"' -f2`
	ACTIVEA=`echo "$LINE" | grep active`
	IPADDRA=`echo "$LINE" | grep lease | awk '{ print $2 }'`
	if [ "$HOSTNAMEA" != "" ]; then
		HOSTNAME="$HOSTNAMEA"
	fi
	if [ "$ACTIVEA" != "" ]; then
		ACTIVE="$ACTIVEA"
	fi
	if [ "$IPADDRA" != "" ]; then
		IPADDR="$IPADDRA"
	fi
	if [ "$HOSTNAME" != "" ]; then
		if [ "$IPADDR" != "" ]; then
			update_hosts_file "$HOSTNAME" "$IPADDR" "$ACTIVE"
			HOSTNAME=""
			ACTIVE=""
			IPADDR=""
		fi
	fi
done
OpenPOWER on IntegriCloud