-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnsmasq-lease-notifier.sh
executable file
·96 lines (70 loc) · 1.91 KB
/
dnsmasq-lease-notifier.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
#!/usr/bin/env bash
# A script that should be executed by dnsmsaq via `--dhcp-script` option.
# It looks for unknown devices and sends notifications via gotify
# depends:
# - nmap (opt, for mac vendor lookup)
# - gotify/cli
set -e
# options:
nmapdb="${DNS_LEASE_WATCHER_NMAP_DB:-/usr/share/nmap/nmap-mac-prefixes}"
# curl http://standards-oui.ieee.org/oui/oui.txt | sudo tee /usr/local/share/oui.txt
ouidb="${DNS_LEASE_WATCHER_OUI_DB:-/usr/local/share/oui.txt}"
# see man dnsmasq, `dhcp-script`
cmd="$1"
mac="$2"
IP="$3"
hostname="$4" # relevant only for `known`
# special tag `known` is set when the host matches one of the `dhcp-host` entries or is in /etc/ethers
# hence we are looking for its absence
if [[ "$DNSMASQ_TAGS" =~ known ]] || [ -z "$DNSMASQ_TAGS" ]; then
exit 0
fi
# we only care about `add` or `old`
if [ "$cmd" = 'del' ]; then
exit 0
fi
dt="$(date '+%b %d %H:%M:%S')"
# ---
search_nmap(){
if [ ! -f "$nmapdb" ]; then
echo "no nmap db" 1>&2
return
fi
sed '/^\s*#/d' "$nmapdb" | rg -i "^$1" | choose 1:
}
search_oui(){
if [ ! -f "$ouidb" ]; then
echo "no oui db" 1>&2
return
fi
rg -i "^$1" "$ouidb" | choose -f $'\t' 1:
}
lookup_vendor() {
# get rid of : separators
local mac="${1//:/}"
local prefix="$(echo "$mac" | head -c 6)"
local vendors=(
"$(search_nmap "$prefix")"
"$(search_oui "$prefix")"
)
vendor='Unknown'
for v in "${vendors[@]}"; do
if [ -n "$v" ]; then
vendor="$v"
fi
done
echo "$vendor"
}
vendor="$(lookup_vendor "$mac")"
out="$dt: new device: $IP"
if [ -n "$DNSMASQ_SUPPLIED_HOSTNAME" ]; then
out="$out supplied host: $DNSMASQ_SUPPLIED_HOSTNAME;"
fi
if [ -n "$DNSMASQ_VENDOR_CLASS" ]; then
out="$out class: $DNSMASQ_VENDOR_CLASS;"
fi
out="$out $mac ($vendor)"
echo "$out"
if command -v gotify >/dev/null; then
echo "$out" | gotify push
fi