What's new

Wifi Presense for Original Asus Firmware

  • 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!

mwolter

New Around Here
First-time poster. Please let me know if anything is in violation.

Created a few scripts to check for association of wireless devices and communicate the status back to a HomeSeer home automation system but it can easily be tailored for other systems. There a few other scripts like this on the internet but this runs on recent Broadcom based Asus Wifi routers and doesn't require merlin. One caveat, you will need to leave a USB device plugged into the router for this to work.

I did my best to condense it as much as possible but I'm not an expert -sh coder so please let me know if there are any errors. This script has been tested for the past few days on a GT-AX11000 and it appears to be working well. I also coded it to use makeshift arrays by using strings and grep. So it should scale to check association for several devices. I'll try to list all steps necessary for this to work properly. All of this will need to be entered from the SSH console, so you will need to become familiar with how to enable and access the console. Hint: Use Putty if windows or SSH if on a Mac.

Enjoy!

1. Start by creating two directories
Code:
mkdir -p /jffs/scripts/wifipresence/supporting/
mkdir -p /jffs/scripts/wifipresence/online/
2. Create the post-mount script. This script will automatically run at boot when a USB device is connected.
Code:
cat > /jffs/post-mount
Paste the following
Code:
#!/bin/sh
sleep 5
#
# add commands below to be run when the router boots if a USB device is connected
#
# copies script into roots crontab if it does not exist
if grep -q "/jffs/scripts/wifipresence/checkpresence.sh" "/var/spool/cron/crontabs/$(nvram get http_username)"; then exit; else cat /jffs/scripts/wifipresence/supporting/cronjob >> /var/spool/cron/crontabs/$(nvram get http_username); fi
Press Ctrl + C keys to exit cat

Make script executable
Code:
chmod a+x /jffs/post-mount
3. Create the script that is copied into root's crontab at boot
Code:
cat > /jffs/scripts/wifipresence/supporting/cronjob
Paste the following
Code:
*/5 * * * * i=0; while [ $i -le 58 ]; do /jffs/scripts/wifipresence/checkpresence.sh; let i=i+1; sleep 5; done
Press Ctrl + C keys to exit cat

4. Create the script that actually checks the association of particular devices on the WiFi network. Add your device names, mac addresses and device reference numbers to the script below before uploading.
Code:
cat > /jffs/scripts/wifipresence/checkpresence.shsleep 5; done
Paste the following
Code:
#!/bin/sh

#logger "presencecheck started"

# String with space between the name of each WiFi device. No spaces in the name itself.
names="Phone1 Phone2"

# Mac address of each WiFi Device. Follow names format.
macs="11:22:33:44:55:66 11:22:33:44:55:77"

# HomeSeer device reference number of each WiFi Device. Follow names format.
refs="40 41"

# function to create a list of mac addresses associated to the WiFi
macaddress() {
    for iface in $(nvram get wl_ifnames); do
        wl -i $iface assoclist | sed 's/assoclist//g'
    done
}

# find number of entries in name
iterations=$( echo "$names" | wc -w )

# directory where to store online status file
dir="/jffs/scripts/wifipresence/online/"

# address and parameters used to communicate status
curladdr="http://USERNAME:PASSWORD@IPADDRESS/JSON?request=controldevicebyvalue&ref="

# value sent if device is online
curlon="&value=100"

# value sent if device is offline
curloff="&value=0"

# start index at 1
i=1

# iternate through name string
while [ "$i" -le "$iterations" ]

do

   # create variables used for awk print
    name=$( echo $names | awk '{print '\$$i'}' )
    mac=$( echo $macs | awk '{print '\$$i'}' )
    ref=$( echo $refs | awk '{print '\$$i'}' )

    # is mac address in list
    if (macaddress | fgrep "$mac" >/dev/null)

            then

                        # if online file does not exist
                        if [ ! -f $dir$name ]

                                then

                                        # create the online file
                                        touch  $dir$name

                                        # communicate the status
                                        curl "$curladdr$ref$curlon" -k >/dev/null 2>&1

                                        # save status in the router's log
                                        logger "$name has connected to WiFi"

                        fi

                else

                        if [ -f  $dir$name ]

                                then

                                        # remove the online file
                                        rm -f $dir$name

                                        # communicate the status
                                        curl "$curladdr$ref$curloff" -k >/dev/null 2>&1

                                        # save status in the router's log
                                        logger "$name has disconnected from WiFi"

                        fi

        fi

        let i=i+1

done

#logger "presencecheck finished"
Press Ctrl + C keys to exit cat

Make script executable
Code:
chmod a+x /jffs/scripts/wifipresence/checkpresence.sh

5. Set script active in NVRAM
Code:
nvram set script_usbmount="/jffs/post-mount"
nvram commit
 
Last edited:

Sign Up For SNBForums Daily Digest

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