Reliable trigger for wireless station disconnect?

Openwrt version:21.02-rc4 on MT7621 based router Xiaomi Redmi RM2100
My intention: When a client connects/disconnects to my router via wifi, I will get an email notification using package like msmtp.
Something like:

if Station1 connects
do email script and send email, stating connection and time
if Station1 disconnects
do email script and send email, stating disconnection and time

Methods tried so far:

  1. /etc/hotplug.d/dhcp and dnsmasq dhcp-script
if [ "$MACADDR" == "aa:aa:aa:aa:aa:aa" ];
then
case "$ACTION" in
	add | update)
  echo -e "Subject:$SUBJ1\r\n\r\nStation connects at $DATE" | /usr/bin/msmtp $EMAIL
	;;
	del)
echo -e "Subject:$SUBJ1\r\n\r\nStation disconnects at $DATE" | /usr/bin/msmtp $EMAIL
	;;
esac
fi

Results: station connects reports correctly, but on disconnecting no email sent. I checked info in this post, "$Action" add | update works but del does not work.

  1. hostapd_cli with -a , from this post
    Invoke demonized mode of hostapd_cli with sth. like
    hostapd_cli -a /location/of/script.sh -B and script below:
#Variables below tested from hostapd_cli
# $1 = wlan interface name
# $2 = AP-STA-CONNECTED/AP-STA-DISCONNECTED
# $3 = mac address

if [ "$3" == "aa:aa:aa:aa:aa:aa" ];
 then
  if [ "$2" == "AP-STA-CONNECTED" ];
	 then
  echo -e "Subject:$SUBJ1\r\n\r\nStation connects at $DATE" | /usr/bin/msmtp $EMAIL
else
  if [ "$2" == "AP-STA-DISCONNECTED" ];
  then
echo -e "Subject:$SUBJ1\r\n\r\nStation disconnects at $DATE" | /usr/bin/msmtp $EMAIL
  fi
 fi

But this works sometimes, not always for reason unknown.

  1. Using logread -f and grep keywords in syslog, but I have no idea how to demonize logread as I need script to run background.

So anything I did wrong? Or I'm missing something?? Pls help if there are any other methods for detecting station disconnection. Thank you.

Try
iw event -t

I did a script that fits this use case. Just fill in your msmtp part into the fifoOut function (which forwards presence state to other scripts via fifo, replace that part).

It can work on one or multiple aps to track if a given device is present.

It uses logread -f , so maybe you want to use it to learn how to do your own.

I don't have any specific advice about the best way to achieve the goal, but I do think you'll want to be really careful about the way you implement such a script, depending on your environment. You may get all sorts of spam from spurious events if you're not careful. For example, when a device is in low power mode, it may power down the radio for short periods of time and then power it back up to do a few tasks, then power down again. Each of these bounce events could trigger an email or other notification, so you'll want to make sure you filter the events within the script to make it a bit more robust and less noisy.

1 Like

Mobile clients may not even disconnect explicitly.
They can leave the coverage and hostapd just drops them on timeout.

1 Like

In any case, these are events that could be transmitted via ubus right? (With a patch of course)
That would allow any app to subscribe to the comings and goings of clients.

Thanks, I tried, results similar to hostapd_cli/dnsmasq dhcp-script, connection event works fine but no disconnection event, strange. I will try other phone and laptop to see if the culprit is on client side.

Good tips, thank you. I only intend to use it in my home instead of production, and my intension is only to receive emails for old and kids of the family for their safety, so I can use hostname in dhcp config as a filter to avoid email flooding, or maybe add some send counter.

Turned out "no disconnection event" is caused by my faulty phone. I tried another phone and laptop and everything works fine.
Now I'm using method #2, if any one searched for this just use the following. Thanks guys.

This method can run in the background and send notification when station/client connects/disconnects.

  1. hostapd_cli with -a , from this post
    Invoke demonized mode of hostapd_cli with sth. like
    hostapd_cli -a /location/of/script.sh -B and script below:
1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.