Skip to content

SimplyProgrammer/OpenWrt-list-client-devices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 

Repository files navigation

OpenWrt-list-client-devices

Lightweight OpenWrt script to show all client devices no matter the connection type (Wired, Wireless, DHCP or Static).

This can be considered a programmatic and better way to LuCi's Status > Routing - IP Neighbors

Example output

IP Addr                      MAC Addr          Vendor (MAC)                       Hostname                  Iface / SSID (dev)     Method     State
-----------------------------------------------------------------------------------------------------------------------------------------------------------
10.1.2.34                    12:45:4e:46:36:ee Uqibuiti Inc                       USW-Flex                  Lvr (eth4)             DHCP       REACHABLE
10.2.4.56                    67:61:67:76:76:68 A SUS INC.                         Pc                        Ntwrk1 (eth3)          DHCP       REACHABLE
172.1.2.101                  82:f9:04:af:f1:b0 *RAND*                             iPhone                    mywicanfi (ap0)        DHCP       STALE
172.1.2.102                  0e:f2:dc:15:f6:fb *RAND*                             -                         mywicanfi (ap0)        Static/?   STALE
172.1.2.105                  e6:98:62:65:fe:2e *RAND*                             iPad                      mywicanfi (ap0)        DHCP       STALE
172.1.2.104                  -                 -                                  -                         mywicanfi (ap0)        Static/?   FAILED
172.1.2.106                  1a:89:54:ad:6c:dc *RAND*                             *                         mywicanfi (ap0)        DHCP       STALE
192.168.3.131                98:8f:e8:62:74:1f Inlet Technology Co.,Ltd.          DESKTOP                   PAP (eth2)             DHCP       STALE
192.168.1.1                  78:9a:18:41:27:c8 Broad.com                          -                         wan (eth0)             Static/?   REACHABLE
192.168.1.223                -                 -                                  -                         wan (eth0)             Static/?   FAILED
192.168.1.248                -                 -                                  -                         wan (eth0)             Static/?   FAILED
192.168.2.105                84:d5:c5:00:a2:db Solar a.s. Technologies            *                         LTv (eth5)             DHCP       STALE
192.168.2.106                47:87:77:99:82:87 King Electrical Appliances         *                         LTv (eth5)             DHCP       STALE

Note that Vendor might not be displayed properly due the the request limits on https://api.macvendors.com when run for first couple of times...

Install

You need to have basic commands (ip, grep, awk, sed etc...) available. Also bash (if you do not have it already):

apk update
apk add bash

Switch to bash and run:

cat << 'THE_END' > /bin/list-clients && chmod +x /bin/list-clients && sed -i -e 's/\t//g' -e '/^[[:space:]]*$/d' -e '/^# /d' /bin/list-clients && grep -qxF '/bin/list-clients' /etc/sysupgrade.conf || echo '/bin/list-clients' >> /etc/sysupgrade.conf
#!/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 show "$@" | 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

THE_END

Now you can simply run it as list-clients and add it as a Custom command (System > Custom commands) to LuCi!<br

  • Additionally, you can send anything that you would use after ip neigh show as arguments. For example list-clients nud reachable will print only reachable devices.

Optionally you can install iw or iwinfo to if you want your wireless SSID to be resolved as fallback for Iface.

Note that command might not work properly if not run with root privileges. Also, the list-clients itself does not require bash to run so after you are done with the install script, you can uninstall bash if you want...

Releases

No releases published

Packages

 
 
 

Contributors

Languages