What's new

User script when device connects to wifi

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

mew1033

Occasional Visitor
Is there a user script that can run whenever a device connects to wifi? I'd like to trigger a shell script on that event.
 
No.
 
Sort of....maybe....
dnsmasq can trigger a user script whenever it assigns an address via DHCP. You would have to then check if the client was connected via wifi....
Check out the dnsmasq doc for dhcp-script
 
And if you might have clients that don't use DHCP, the other method people used in the past was to create a cron job that polls the list of associated clients, reacting whenever a new one appears on the list. Might be a delay involved (if for instance you poll every minute), but it would be more accurate.

You could also potentially look at the ARP cache content, but that won't allow you to distinguish wired from wireless clients.
 
And if you might have clients that don't use DHCP, the other method people used in the past was to create a cron job that polls the list of associated clients, reacting whenever a new one appears on the list. Might be a delay involved (if for instance you poll every minute), but it would be more accurate.
Something like this, but it would need to be adapted for non-Broadcom chipsets.

/jffs/scripts/services-start
Code:
#!/bin/sh

/jffs/scripts/wireless_monitor.sh &
/jffs/scripts/wireless_monitor.sh
Code:
#!/bin/sh

oldlist=/tmp/assoclist.old
newlist=/tmp/assoclist.new
touch $oldlist          # Create an empty file the first time through

while sleep 15
do
        for iface in $(nvram get wl_ifnames)
        do
                wl -i $iface assoclist | awk '{print $2}' >> $newlist
        done

        for macaddr in $(grep -vxFf $oldlist $newlist)
        do
                arpinfo="$(arp -a | grep -iF $macaddr | awk '{print $1 " " $2}')"
                logger -t wireless $macaddr $arpinfo "has connected."
        done

        for macaddr in $(grep -vxFf $newlist $oldlist)
        do
                arpinfo="$(arp -a | grep -iF $macaddr | awk '{print $1 " " $2}')"
                logger -t wireless $macaddr $arpinfo "has disconnected."
        done

        mv $newlist $oldlist
done
 
Awesome, thanks a lot! These are all good options. I'm migrating from DD-WRT on an old router, and I was using
Code:
wl_atheros assoclist | grep $MAC_ADDRESS
every 5 seconds. I figured I could adapt that to asuswrt merlin, but I wanted to make sure there wasn't a better, more supported way of doing it.
If the dhcp-script from dnsmasq is pretty quick, that might be a good option. I'm looking for a specific MAC address (a cell phone), so I wouldn't need dnsmasq to tell me if it came from wireless or wired.

Thanks again
 
And if you might have clients that don't use DHCP, the other method people used in the past was to create a cron job that polls the list of associated clients, reacting whenever a new one appears on the list. Might be a delay involved (if for instance you poll every minute), but it would be more accurate.

You could also potentially look at the ARP cache content, but that won't allow you to distinguish wired from wireless clients.

I have to agree with Eric here...

Not worth doing...
 

Latest threads

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!
Top