-
Notifications
You must be signed in to change notification settings - Fork 9
/
helper_dhcpstaticlist.sh
executable file
·158 lines (120 loc) · 6.18 KB
/
helper_dhcpstaticlist.sh
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/sh
####################################################################################################
# Original Script: dhcpstaticlist.sh
# Original Author: Xentrk
# Last Updated Date: 4-January-2019
# Compatible with 384.15
# modified by Corgan
####################################################################################################
# Uncomment for debugging
#set -x
readonly SCRIPT_NAME="extstats"
readonly SCRIPT_NAME_LOWER=$(echo $SCRIPT_NAME | tr 'A-Z' 'a-z' | sed 's/d//')
readonly SCRIPT_DIR="/jffs/addons/$SCRIPT_NAME_LOWER.d"
readonly SCRIPT_CONF="$SCRIPT_DIR/config.conf"
readonly DHCP_HOSTNAMESMAC="/opt/tmp/dhcp_clients_mac.txt"
readonly DHCP_HOSTNAMESMAC_CSV="/opt/tmp/dhcp_clients_mac.csv"
readonly DHCP_HOSTNAMESMAC_SB_IP="/opt/tmp/dhcp_clients_mac_sb_ip.txt"
readonly DHCP_HOSTNAMESMAC_SB_MAC="/opt/tmp/dhcp_clients_mac_sb_mac.txt"
readonly DHCP_HOSTNAMESMAC_SB_HOST="/opt/tmp/dhcp_clients_mac_sb_host.txt"
readonly CLIENTLIST="/opt/tmp/client-list.txt"
readonly CLIENTLIST_CSV="/opt/tmp/client-list.csv"
readonly DHCP_EXTERNAL="/opt/tmp/dhcp_external.csv"
readonly EXTS_MAC_LIST_URL=$(grep "EXTS_MAC_LIST_URL" "$SCRIPT_CONF" | cut -f2 -d"=")
Parse_Hostnames() {
true >/tmp/hostnames.$$
OLDIFS=$IFS
IFS="<"
for ENTRY in $HOSTNAME_LIST; do
if [ "$ENTRY" = "" ]; then
continue
fi
MACID=$(echo "$ENTRY" | cut -d ">" -f 1)
HOSTNAME=$(echo "$ENTRY" | cut -d ">" -f 2)
echo "$MACID $HOSTNAME" >>/tmp/hostnames.$$
done
IFS=$OLDIFS
}
get_clientlist() {
rm -f $CLIENTLIST
rm -f $CLIENTLIST_CSV
#CSV
#header needed
echo "hostname,mac" > $CLIENTLIST_CSV
LIST=$(nvram get custom_clientlist)
OLDIFS=$IFS
IFS="<"
for CLIENT_ENTRY in $LIST; do
if [ "$CLIENT_ENTRY" = "" ]; then
continue
fi
CLIENT_MACID=$(echo "$CLIENT_ENTRY" | cut -d ">" -f 1)
CLIENT_HOSTNAME=$(echo "$CLIENT_ENTRY" | cut -d ">" -f 2)
if [ "$CLIENT_HOSTNAME" != "" ]; then
echo "$CLIENT_MACID $CLIENT_HOSTNAME"
echo "$CLIENT_MACID $CLIENT_HOSTNAME" >>$CLIENTLIST
#csv
echo "$CLIENT_HOSTNAME,$CLIENT_MACID" >>$CLIENTLIST_CSV
fi
done
IFS=$OLDIFS
}
Save_Dnsmasq_Format() {
# Obtain MAC and IP address from dhcp_staticlist and exclude DNS field by filtering using the first three octets of the lan_ipaddr
if [ -s /jffs/nvram/dhcp_staticlist ]; then #HND Routers store dhcp_staticlist in a file
awk '{print $0}' /jffs/nvram/dhcp_staticlist | grep -oE "((([0-9a-fA-F]{2})[ :-]){5}[0-9a-fA-F]{2})|(([0-9a-fA-F]){6}[:-]([0-9a-fA-F]){6})|([0-9a-fA-F]{12})" >/tmp/static_mac.$$
awk '{print $0}' /jffs/nvram/dhcp_staticlist | grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | grep "$(nvram get lan_ipaddr | grep -Eo '([0-9]{1,3}\.[0-9]{1,3}(\.[0-9]{1,3}))')" >/tmp/static_ip.$$
else # non-HND Routers store dhcp_staticlist in nvram
nvram get dhcp_staticlist | grep -oE "((([0-9a-fA-F]{2})[ :-]){5}[0-9a-fA-F]{2})|(([0-9a-fA-F]){6}[:-]([0-9a-fA-F]){6})|([0-9a-fA-F]{12})" >/tmp/static_mac.$$
#nvram get custom_clientlist | grep -oE "((([0-9a-fA-F]{2})[ :-]){5}[0-9a-fA-F]{2})|(([0-9a-fA-F]){6}[:-]([0-9a-fA-F]){6})|([0-9a-fA-F]{12})" >/tmp/static_mac.$$
nvram get dhcp_staticlist | grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | grep "$(nvram get lan_ipaddr | grep -Eo '([0-9]{1,3}\.[0-9]{1,3}(\.[0-9]{1,3}))')" >/tmp/static_ip.$$
#nvram get custom_clientlist | grep -oE "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" | grep "$(nvram get lan_ipaddr | grep -Eo '([0-9]{1,3}\.[0-9]{1,3}(\.[0-9]{1,3}))')" >/tmp/static_ip.$$
fi
# output /tmp/static_mac.$$ and /tmp/static_ip.$$ to /tmp/staticlist.$$ in two columns side by side
#https://www.unix.com/shell-programming-and-scripting/161826-how-combine-2-files-into-1-file-2-columns.html
awk 'NR==FNR{a[i++]=$0};{b[x++]=$0;};{k=x-i};END{for(j=0;j<i;) print a[j++],b[k++]}' /tmp/static_mac.$$ /tmp/static_ip.$$ >/tmp/staticlist.$$
# some users reported <undefined in nvram..need to remove
if [ -s /jffs/nvram/dhcp_hostnames ]; then #HND Routers store hostnames in a file
HOSTNAME_LIST=$(awk '{print $0}' /jffs/nvram/dhcp_hostnames | sed 's/>undefined//')
else
HOSTNAME_LIST=$(nvram get dhcp_hostnames | sed 's/>undefined//')
fi
# Have to parse by internal field separator since hostnames are not required
Parse_Hostnames
# Join the /tmp/hostnames.$$ and /tmp/staticlist.$$ files together to form one file containing MAC, IP, HOSTNAME
awk '
NR==FNR { k[$1]=$2; next }
{ print $0, k[$1] }
' /tmp/hostnames.$$ /tmp/staticlist.$$ >/tmp/MACIPHOSTNAMES.$$
# write dhcp-host entry in /jffs/configs/dnsmasq.conf.add format
#sort -t . -k 3,3n -k 4,4n /tmp/MACIPHOSTNAMES.$$ | awk '{ print "dhcp-host="$1","$2","$3""; }' | sed 's/,$//'
sort -t . -k 3,3n -k 4,4n /tmp/MACIPHOSTNAMES.$$ | awk '{ print ""$3" "$1" "$2""; }' | sed 's/,$//'
sort -t . -k 3,3n -k 4,4n /tmp/MACIPHOSTNAMES.$$ | awk '{ print ""$3" "$1" "$2""; }' | sed 's/,$//' > $DHCP_HOSTNAMESMAC
#CSV
rm $DHCP_HOSTNAMESMAC_CSV
#header needed
echo "hostname,mac" > $DHCP_HOSTNAMESMAC_CSV
sort -t . -k 3,3n -k 4,4n /tmp/MACIPHOSTNAMES.$$ | awk '{ print ""$3","$1""; }' | sed 's/,$//'
sort -t . -k 3,3n -k 4,4n /tmp/MACIPHOSTNAMES.$$ | awk '{ print ""$3","$1""; }' | sed 's/,$//' >> $DHCP_HOSTNAMESMAC_CSV
#cleanup
rm -rf /tmp/static_mac.$$
rm -rf /tmp/static_ip.$$
rm -rf /tmp/staticlist.$$
rm -rf /tmp/hostnames.$$
rm -rf /tmp/MACIPHOSTNAMES.$$
#save copys sorted
sort -k 1,1 -s $DHCP_HOSTNAMESMAC > $DHCP_HOSTNAMESMAC_SB_HOST
sort -k 2,2 -s $DHCP_HOSTNAMESMAC > $DHCP_HOSTNAMESMAC_SB_MAC
sort -k 3,3 -s $DHCP_HOSTNAMESMAC > $DHCP_HOSTNAMESMAC_SB_IP
}
get_external_list()
{
curl -sS "$EXTS_MAC_LIST_URL" 2>/dev/null -o "$DHCP_EXTERNAL"
}
Save_Dnsmasq_Format
get_clientlist
if [ ! -z "$EXTS_MAC_LIST_URL" ] ; then
get_external_list
fi
# nvram get custom_clientlist
# cat /var/lib/misc/dnsmasq.leases