ElBandolero
Posts: 2
Joined: Wed Dec 06, 2017 11:37 am

detecting when a node is connected to hostapd and use this to trigger a script

Wed Dec 06, 2017 11:43 am

hello,

I am new to the raspberry scene and I have an idea to create an energy efficient Wi-Fi router using the pi 3 I have managed to gather all the necessary tools including hostapd although I cant figure out if there is a particular section within Raspbian OS which stroes the connected devices or how it knows when a device is connected so that I could use this as a trigger for a script to run (When a node connects the pi will run as normal and when a node disconnects the pi will run at a lower powered state) please help!!!!!

SurferTim
Posts: 1769
Joined: Sat Sep 14, 2013 9:27 am
Location: Miramar Beach, Florida

Re: detecting when a node is connected to hostapd and use this to trigger a script

Wed Dec 06, 2017 1:47 pm

Add this to /etc/hostapd/hostapd.conf

Code: Select all

ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
Then use this to determine clients.

Code: Select all

sudo hostapd_cli all_sta
You will need to parse this output.

ElBandolero
Posts: 2
Joined: Wed Dec 06, 2017 11:37 am

Re: detecting when a node is connected to hostapd and use this to trigger a script

Wed Dec 06, 2017 7:46 pm

thank you, I was struggling to even find out how to see connected devices :) although let's say I want to use this information to then trigger a script when a node is connected and trigger another script when no nodes are connected how would I go about this? (sorry really new to the Pi and Raspbian) thanks again

epoch1970
Posts: 3887
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: detecting when a node is connected to hostapd and use this to trigger a script

Wed Dec 06, 2017 8:54 pm

Well, you'll need:
a) To run your script from time to time, automatically
b) To "Parse the output" of the "all_sta" command, as SurferTim said: look into the documentation for what those lines mean, grab the ones you want, and then do your processing.

FWIW, I have such a script that runs every 10 minutes on AP machines. It lists the number of clients, and for each the (obfuscated) MAC, hostname, make, connected time. That list is then displayed on a web page. It's written in bash, not pretty and not portable.

Code: Select all

#!/bin/bash
# Display basic information about clients connected to our AP
# Requires ieee-data among other packages
DEBUG=1
ME="wapreport"
LOGGER="/usr/bin/logger"
#LOGGEROPTS="-t $ME -i --"   # Logs to syslog
LOGGEROPTS="-t $ME -i -s --" # Mirrors to stdout

# The wireless interface to query and bridge we can arp against
WAP_IFACE=$1
ARP_IFACE='br0'
[ "$WAP_IFACE" == "" ] && $LOGGER $LOGGEROPTS "This script takes a wifi interface name as argument. Exit" && exit

# Survey
DATA=$(/usr/bin/sudo /usr/sbin/hostapd_cli -i ${WAP_IFACE} all_sta);
ARPTABLE=$(/usr/sbin/arp -i ${ARP_IFACE});

# Get the number of clients and connected time
mapfile -t STAS < <(/bin/echo "${DATA}" | /bin/grep -o -E '^([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' )
COUNT=${#STAS[@]}
mapfile -t CONNTIMES < <(/bin/echo "${DATA}" | /bin/grep -o -P '(?<=connected_time=).*')
# Try to identify clients
IDS=()
OUIS=();
for STA in "${STAS[@]}"
do
    IDS+=( $(/bin/echo "${ARPTABLE}" | /bin/grep -o -P ".*(?=\s+ether\s+${STA})") )
    OUI_b16=${STA//:/}
    MAKE=$(/bin/grep -i "${OUI_b16:0:6}" /var/lib/ieee-data/oui.txt | cut -d$'\t' -f3 | sed -z -E 's/\r|\n//g')
    [[ "${MAKE}" == "" ]] && MAKE="Unknown"
    OUIS+=("${MAKE}")
done
# Now we return our count and lists
echo $COUNT
for (( i=0; i<${COUNT}; i++)); do
    # We obfuscate the MAC
    STA=$(echo "${STAS[$i]}" | /bin/sed -e 's/\(.\{9\}\)\(.\{5\}\)/\1xx:xx/')
    echo "${STA} | ${IDS[$i]} | ${OUIS[$i]} | ${CONNTIMES[$i]}"
done
exit
It runs like this:

Code: Select all

[email protected]:~ $ ./wapsurvey.sh wap0
2
78:e4:00:xx:xx:0a | montmartre.lan | Hon Hai Precision Ind. Co.,Ltd. | 324
7c:c3:a1:xx:xx:77 | odessa.lan | Apple, Inc. | 2437
[email protected]:~ $
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel


Return to “Networking and servers”