Repository navigation
Expand file tree
/
Copy pathlist-clients
More file actions
68 lines (54 loc) · 2.23 KB
/
Copy pathlist-clients
File metadata and controls
68 lines (54 loc) · 2.23 KB
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
#!/bin/sh
LEASES="/tmp/dhcp.leases"
ETHS="/etc/ethers"
MAC_VENDORS="/etc/mac-vendors.db"
# Mac vendor cahce
MAC_CACHE=$(cat "$MAC_VENDORS" 2>/dev/null)
# Print header
printf "%-28s %-17s %-34s %-25s %-22s %-10s %-10s\n" "IP Addr" "MAC Addr" "Vendor (MAC)" "Hostname" "Iface / SSID (dev)" "Method" "State"
printf %155s | tr \ -
echo
elap=0
ip neigh | sort | while read -r l; do
ip=$(echo "$l" | awk '{print $1}')
ifc=$(echo "$l" | sed -n 's/.* dev \([^ ]*\).*/\1/p')
mac=$(echo "$l" | sed -n 's/.* lladdr \([^ ]*\).*/\1/p')
state=$(echo "$l" | awk '{print $NF}')
[ -z "$ip" ] && ip="-"
[ -z "$ifc" ] && ifc="-"
[ -z "$state" ] && state="-"
# Hostname & Method detection
host=$(awk -v mac="$mac" '$2 == mac {print $4}' "$LEASES" 2>/dev/null)
meth="Static/?"
if [ -n "$host" ]; then
meth="DHCP"
elif grep -qi "^[[:space:]]*$mac[[:space:]]" "$ETHS" 2>/dev/null; then
meth="Static"
[ -z "$host" ] && host=$(awk -v mac="$mac" 'BEGIN{IGNORECASE=1} $1==mac {print $2}' "$ETHS" 2>/dev/null)
fi
[ -z "$host" ] && host="-"
# Mac vend lookup with local cache
vend="-"
pref=$(echo "$mac" | tr '[:lower:]' '[:upper:]' | cut -d: -f1-3)
[ -n "$pref" ] && vend=$(echo "$MAC_CACHE" | grep -i "^$pref=" | head -n1 | cut -d= -f2-)
if [ -z "$vend" ] && [ "$pref" != "---" ] && [ "$elap" -lt 4 ]; then
t0=$(date +%s)
jsn=$(wget -qO- "https://api.maclookup.app/v2/macs/$mac" 2>/dev/null)
elap=$(($(date +%s) - t0))
resp=$(printf "%s" "$jsn" | sed -E 's/.*"company": *"([^"]*)".*/\1/')
[ -z "$resp" ] && [ "$(printf "%s" "$jsn" | sed -E 's/.*"isRand": *([^,}]*).*/\1/')" = "true" ] && resp="*RAND*"
if [ -n "$resp" ]; then
vend="$resp"
echo "$pref=$vend" >> "$MAC_VENDORS"
MAC_CACHE="${MAC_CACHE}${MAC_CACHE:+
}$pref=$vend"
fi
fi
[ -z "$mac" ] && mac="-"
[ -z "$vend" ] && vend="-"
owrtIfc=$(uci show network 2>/dev/null | grep "$ifc" | cut -d. -f2 | cut -d= -f1 | head -n1)
[ -z "$owrtIfc" ] && owrtIfc=$(iwinfo "$ifc" i 2>/dev/null | sed -n 's/.*ESS..: "\(.*\)".*/\1/p')
[ -z "$owrtIfc" ] && owrtIfc=$(iw dev "$ifc" info 2>/dev/null | awk -F 'ssid ' '/ssid/ {print $2}')
[ -z "$owrtIfc" ] && owrtIfc="-"
printf "%-28s %-17s %-34.34s %-25.25s %-22.22s %-10s %-10s\n" "$ip" "$mac" "$vend" "$host" "$owrtIfc ($ifc)" "$meth" "$state"
done