Skip to content

Instantly share code, notes, and snippets.

@jxmot
Created May 7, 2018 15:28
Show Gist options
  • Save jxmot/df091eca6b2dec8ce4dc7dcb5afed4ee to your computer and use it in GitHub Desktop.
Save jxmot/df091eca6b2dec8ce4dc7dcb5afed4ee to your computer and use it in GitHub Desktop.
Tessel 2 & OpenWRT - Show AP WiFi Clients

Tessel 2 & OpenWRT - Show AP WiFi Clients

The show_wifi_clients.sh shell script found here is a modified version of what I found at the OpenWRT Wireless FAQ.

My intent is to use it on a Tessel 2 along with some modifications its firmware. The firmware mods (not shown here) will exec the script to obtain a JSON formatted list of stations that are attached to the Tessel's access point.

The differences are -

  • Can optionally create output in JSON
    • Includes the time stamp found in the DHCP lease file
  • Minor changes made to expressions

Default Ouput Example -

> ./show_wifi_clients.sh
# All connected wifi devices, with IP address,
# hostname (if available), and MAC address.
# IP address    name    MAC address
192.168.1.173   ESP_DDEEFF      aa:bb:cc:dd:ee:ff
192.168.1.158   android-2496ac597fe00412        00:11:22:33:44:55

JSON Formatted Output Example -

> ./show_wifi_clients.sh json
[
{"tstamp":1525747482,"ip":"192.168.1.173","host":"ESP_DDEEFF","mac":"aa:bb:cc:dd:ee:ff"},
{"tstamp":1525747710,"ip":"192.168.1.158","host":"android-2496ac597fe00412","mac":"00:11:22:33:44:55"}
]
#!/bin/sh
# Originally obtained from :
# https://openwrt.org/docs/guide-user/network/wifi/faq.wireless
#
# Modified By :
# https://github.com/jxmot
#
# Used for a Tessel 2 project :
# https://github.com/jxmot/tessel-networking-example
#
# Usage :
#
# Create a table of stations :
# ./show_wifi_clients.sh
#
#
# Create an array of JSON strings. One element for each station
# ./show_wifi_clients.sh json
#
#
# look for the "json" argument, and enable JSON output
# if present.
jsonout=0
if [ "$1" = "json" ]; then
jsonout=1
fi
# Shows MAC, IP address and any hostname info for all connected wifi devices
# written for openwrt 12.09 Attitude Adjustment
if [ $jsonout -eq 0 ]; then
echo "# All connected wifi devices, with IP address,"
echo "# hostname (if available), and MAC address."
echo -e "# IP address\tname\tMAC address"
else
# output format is JSON, start an array
echo "["
fi
# list all wireless network interfaces
# (for MAC80211 driver; see wiki article for alternative commands)
for interface in $(iw dev | grep Interface | cut -f 2 -s -d" ")
do
# for each interface, get mac addresses of connected stations/clients
maclist=$(iw dev $interface station dump | grep Station | cut -f 2 -s -d" ")
# count the MACs that were found
mcount=$(echo "$maclist" | wc -w)
# for each mac address in that list...
for mac in $maclist
do
# If a DHCP lease has been given out by dnsmasq,
# save it.
ip="UNKN"
host=""
ip=$(cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 2 -s -d" ")
host=$(cat /tmp/dhcp.leases | cut -f 2,3,4 -s -d" " | grep $mac | cut -f 3 -s -d" ")
# ... show the mac address:
if [ $jsonout -eq 0 ]; then
echo -e "$ip\t$host\t$mac"
else
# output data as JSON
tstamp=$(cat /tmp/dhcp.leases | grep $mac | cut -f 1 -s -d" ")
echo -n "{\"tstamp\":$tstamp,\"ip\":\"$ip\",\"host\":\"$host\",\"mac\":\"$mac\"}"
# decrement the count, if > 0 then append a comma
mcount=$((mcount - 1))
if [ $mcount -gt 0 ]; then
echo ","
else
echo ""
fi
fi
done
done
# close the JSON array
if [ $jsonout -eq 1 ]; then
echo "]"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment