I use an RPI3 as a standalone access point (following this tutorial) and I would like to make it execute a bash script when a device is connected to its wireless network. What is the appropriate way to do this?
By searching I found a solution.
Step 1: Create a file named onHostapdChange.sh
nano onHostapdChange.sh
Step 2: In the file type or copy-paste the lines...
#!/bin/bash
if [[ $2 == "AP-STA-CONNECTED" ]]
then
echo "someone has connected with mac id $3 on $1"
fi
if [[ $2 == "AP-STA-DISCONNECTED" ]]
then
echo "someone has disconnected with mac id $3 on $1"
fi
... then save (Ctrl + O) and exit nano (Ctrl + X).
Step 3. Make it executable:
chmod +x onHostapdChange.sh
Step 4. Execute the command
sudo hostapd_cli -a '/onHostapdChange.sh'
If the error message 'Failed to connect to hostapd - wpa_ctrl_open: No such file or directory' appears then edit the file '/etc/hostapd/hostapd.conf'...
sudo nano /etc/hostapd/hostapd.conf
...in the end of the file append the lines:
ctrl_interface=/var/run/hostapd
ctrl_interface_group=0
...and restart the service:
sudo systemctl stop hostapd
sudo systemctl start hostapd
Instructions (combined) from