How to listen to events such as when a device connects or disconnects from the network?

I wanna make a script which would live inside the router which would turn on wifi, when it detects that my macbook has been disconnected from the LAN.
And vice versa, I wanna turn off the wifi, when the macbook is connected to LAN.

My question is, is there an effective way on how to listen to the connect/disconnect events?
I would like to avoid creating some custom intervals which would check arp and dhcp tables.

I heard some use pakon for this, but that’s probably more suitable for manual inspection (and doesn’t have event hooks AFAIK). dnsmasq provides event hook for DHCP(v4).

You could modify this package made for updating home assistant with device status.

It’s using hostapd_cli.

Edit: Sorry I misread your post. The package I suggested will only monitor wireless connections.

thanks for your comments. None of these seamed usable, so I ended up with a while loop with 3 second pause in it. Here is the script if anyone is interested in this as well

unset IS_MAC_ON_LAN
MAC_ADDRESS=de:ad:be:ef:00:00.


PREVIOUS_WIFI_STATE=firstRun

while [ 1=1 ]
do
for ip in $(arp | grep -v IP | awk '{print $1}'); do 
    IS_MAC_ON_LAN=${IS_MAC_ON_LAN}$(grep $ip /tmp/dhcp.leases | grep $MAC_ADDRESS);
done

echo $IS_MAC_ON_LAN

if [ -n "$IS_MAC_ON_LAN" ]; then
    if [[ "$PREVIOUS_WIFI_STATE" == "up" || "$PREVIOUS_WIFI_STATE" == "firstRun" ]]; then
        echo 'mac is on LAN - turning off wifi'
        wifi down
        PREVIOUS_WIFI_STATE=down
    fi
else
    if [[ "$PREVIOUS_WIFI_STATE" == "down" || "$PREVIOUS_WIFI_STATE" == "firstRun" ]]; then
        echo 'mac is NOT on LAN - turning on wifi'
        wifi up
        PREVIOUS_WIFI_STATE=up
    fi
fi
sleep 3
done

I could run this script by pasting it into terminal and executing it with no problem.
But there is an issue if this script is saved as a .sh file
then it gives me error with “arp: command not found”
I tried which arp to locate it, but it doesn’t return a path for unknown reason.

Does somebody know how I can run arp from inside a .sh file?
Thanks

There may be a better way, no need to poll, assuming your dhcp lease is relatively short.

Modify dnsmasq.conf, add parameter dhcp-script=/path/to/yourscript.sh

Make a script along the lines of:

#!/bin/sh

# script to detect new dhcp lease

# this will be called by dnsmasq everytime a new device is connected
# with the following arguments
# $1 = add or old
# $2 = mac address
# $3 = ip address
# $4 = device name

I got inspired by a random github gist.

thanks, this sounds good, but I failed to do this.
There are two dnsmasq.conf files in turris
/tmp/etc/dnsmasq.conf (this one is generated from /etc/config/dhcp)
/etc/dnsmasq.conf

I have added a new line in them
dhcp-script=/run/scripts/manageWifi.sh

It didn’t work (I also tried restarting the router)
So I added it also to /etc/config/dhcp
option dhcp-script '/run/scripts/manageWifi.sh'

tried after restarting. Reconnecting the network devices does not trigger that dhcp-script.

I tried to debug it with touch /run/scripts/HelloWorld inside the dhcp script, but that file never gets created automatically when reconnecting the network devices.


Another things is, that the default DHCP lease time for LAN in turris is 12h
I have tried to do change this to 5s

1 Like

Did you test running your script from the command line? (is it executable?)

You shouldn’t restart the router, just do /etc/init.d/dnsmasq restart or similar.

Also, the DHCP lease in seconds is not safe because the clients will permanently ask for addresses and it may result in outage if they don’t get the proper response back.