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?

1 Answer 1


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...

if [[ $2 == "AP-STA-CONNECTED" ]]
  echo "someone has connected with mac id $3 on $1"

if [[ $2 == "AP-STA-DISCONNECTED" ]]
  echo "someone has disconnected with mac id $3 on $1"

... 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:


...and restart the service:

sudo systemctl stop hostapd
sudo systemctl start hostapd

Instructions (combined) from

  1. https://superuser.com/questions/1071354/hostapd-execute-a-command-when-there-is-new-connection-established

  2. https://www.raspberrypi.org/forums/viewtopic.php?t=63045

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.